One Exception handler for all exceptions of a CLASS

前端 未结 6 1635
甜味超标
甜味超标 2021-02-04 01:05

I have a class with number of methods and want to have one exception handler for them all. There are so many of these methods and they have different parameters, that it would b

相关标签:
6条回答
  • 2021-02-04 01:27

    This sounds like a problem with your design. Can you elaborate on exactly what exceptions you are trying to catch and why and we can try and help with that.

    0 讨论(0)
  • 2021-02-04 01:30

    I can't figure out any reason why you may benefit from handling all the exceptions in a class using a single method (can you elaborate? I'm curious...)

    Anyway, you can use AOP (Aspect Oriented Programming) techniques to inject (statically or at runtime) exception handling code around the methods of your class.

    There's a good assembly post-processing library called PostSharp that you can configure using attributes on the methods in your class:

    You may define an aspect like this (from PostSharp website):

    public class ExceptionDialogAttribute : OnExceptionAspect
    {
        public override void OnException(MethodExecutionEventArgs eventArgs)
        {
            string message = eventArgs.Exception.Message;
            MessageBox.Show(message, "Exception");
            eventArgs.FlowBehavior = FlowBehavior.Continue;
        }
    }
    

    And then you'll apply the attribute to the methods you want to watch for exceptions, like this:

    public class YourClass {
    
        // ...
    
        [ExceptionDialog]
        public string DoSomething(int param) {
            // ...
        }
    }
    

    You can also apply the attribute to the whole class, like this:

    [ExceptionDialog]
    public class YourClass {
        // ...
        public string DoSomething(int param) {
            // ...
        }
        public string DoSomethingElse(int param) {
            // ...
        }
    }
    

    This will apply the advice (the exception handling code) to every method in the class.

    0 讨论(0)
  • 2021-02-04 01:30

    you might want to put a try/catch about you main method, although i consider this to be a serious misuse of exception handling, except you want to append a logger or sth.

    0 讨论(0)
  • 2021-02-04 01:39

    You could use a delegate to pass your method's code into a single try catch like the following example:

        private void GlobalTryCatch(Action action)
        {
            try
            {
                action.Invoke();
            }
            catch (ExpectedException1 e)
            {
                throw MyCustomException("Something bad happened", e);
            }
            catch (ExpectedException2 e)
            {
                throw MyCustomException("Something really bad happened", e);
            }
        }
    
        public void DoSomething()
        {
            GlobalTryCatch(() =>
            {
                // Method code goes here
            });
        }
    
    0 讨论(0)
  • 2021-02-04 01:39

    Exceptions are not really class related but method/callstack oriented. An object should, in general, not try to handle exceptions from it's own methods. It's up to the callers of those methods.

    0 讨论(0)
  • 2021-02-04 01:45

    I don't think there is. You could move the try/catch to the caller, but that's not very good design. It may be better to separate it out into another class, and use Reflection to call the methods, like this:

    public class MyClass {}
    public class MySafeClass {
        public void CallMethod(string name, object[] param) {
            Type t = typeof(MyClass);
            MyClass mc = new MyClass();
            try {
                t.GetMethod(name).Invoke(mc, param);
            }
            catch {
                //...;
            }
        }
    

    }

    But you shouldn't! It's not very good practice.

    Another method is still using try/catch but having a single method to throw exceptions, etc back to the user:

    public class MyClass {
        void DoException(string message) {
            throw new Exception(message);
        }
    }
    

    But that still isn't that good an option.

    I don't see why it would be ugly - even if you just wrap the whole method in one try/catch with a message. That might be feasible.

    It's also a better option to just leave them and pass them back up to the caller, perhaps in try/finally.

    It's not exactly hard to try/catch everything, especially with the snippets in Visual Studio and SharpDevelop.

    0 讨论(0)
提交回复
热议问题