Catch-all error handling on application level?

前端 未结 5 1871
面向向阳花
面向向阳花 2021-01-19 04:58

I have a WPF form with a couple of buttons, and for each button I have error handling code:

try {bla bla} 
catch(Exception e){
  more bla
}

相关标签:
5条回答
  • 2021-01-19 05:11

    If it is a WinForms app, you can encapsulate the Application.Run() call in a try catch statement, although I really wouldn't recommend this, you should only be using try catches where they are absoloutely needed and deal with exceptions whereever possible before just relying on catches.

    I know this method works though as I had to use it temporarily once to do some cleaning up on the crashing of an application.

    0 讨论(0)
  • 2021-01-19 05:18

    There are multiple possibilities:

    1. Suggestions for making a reusable try/catch block in C#?
    2. If you have a Winforms application, subscribe to the events Application.ThreadException and AppDomain.CurrentDomain.UnhandledException: http://www.csharp-examples.net/catching-unhandled-exceptions/
    3. If you have a WPF application, subscribe to Application.DispatcherUnhandledException and AppDomain.CurrentDomain.UnhandledException
    0 讨论(0)
  • The answer depends on your platform. In a web application you can bind the Application_Error event in Global Asax. In WCF you can inject an error handler of type System.ServiceModel.Dispatcher.IErrorHandler into the WCF stack, in forms applications you can bind the ThreadException event.

    Using it may imho be a good idea in some situations since you prevent showing exception details to the user, but also indicates a certain slobbyness regarding exception handling. I have used WCF error handlers to convert domain exceptions to http status code which is simple. Whether it is a good practice or not I do not know. For asp.net applications it is also worth looking at elmah available via NuGet.

    It is also possible to write a simple exception handler that allows you to repeat the try/catch blocks by sending reoutines as Func or Action like this

        var eh = new ExceptionHandler();
    
        eh.Process ( () => throw new SillyException());
    

    with class ExceptionHandler

        class ExceptionHandler 
        { 
            public T Process(Func<T> func()) 
            {
               try { return func(); }
               catch(Exception ex) { // Do stuff }
            }
    
            public void Process(Action a) 
            { 
                try { action() }
                catch(Exception ex) { // Do stuff }
            }
        }
    
    0 讨论(0)
  • 2021-01-19 05:28

    You can subscribe to AppDomain.UnhandledException event (see MSDN) to accomplish this. Ideally, this happens when bootstrapping your application. You can find the AppDomain you're executing in via AppDomain.Current.

    0 讨论(0)
  • 2021-01-19 05:36

    Add this to you Global.asax:

    protected void Application_Error(object sender, EventArgs e) {
        Exception currentException = Server.GetLastError();
    
        // your handling code goes here ...
    
        Server.ClearError();
    }
    
    0 讨论(0)
提交回复
热议问题