WPF captures unprocessed exceptions and WPF captures and processes them.
In WPF, try/catch blocks are generally used for exception capture. Like bugs in a program, it is difficult to ensure that all exceptions in the program can be captured through try/catch. If exceptions are not captured, the user experience will be compromised. In severe cases, data loss will occur. The Application. DispatcherUnhandledException event and AppDomain. UnhandledException event are provided in WPF. By registering these two events, we can perform custom processing on unprocessed global exceptions in a centralized manner.
For every exception not handled by the code running on the main UI thread, the Application will trigger a DispatcherUnhandledException. When you handle an unhandled exception from DispatcherUnhandledException and do not want WPF to continue to handle the exception, set the Handled attribute to true. In general, if you do not want the application to crash, we need to set the Handled attribute to true. However, not all exceptions can be recovered. If the exception is FileNotFoundException, the program can continue to run after the exception is handled. If the exception is StackOverflowException, the program cannot continue to run and is about to close.
UnhandledException is triggered for any unprocessed exceptions in any thread and exceptions in any application domain. If the exception in the UI thread is not handled, UnhandledException is also triggered. Starting from. NET Framework 4, a corrupted process state exception does not cause this event, such as stack overflow or access conflict. By default, the Common Language Runtime (CLR) does not output these exceptions to managed code and does not call try/catch blocks for them.
Simple code:
1 // <summary> 2 // start the application 3 /// </summary> 4 /// <param name = "sender"> </param> 5/ // <param name = "e"> </param> 6 private void Application_Startup (object sender, startupEventArgs e) 7 {8 Current. dispatcherUnhandledException + = App_OnDispatcherUnhandledException; 9 AppDomain. currentDomain. unhandledException + = CurrentDomain_UnhandledException; 10} 11 12 /// <summary> 13 // The UI thread throws a global exception to handle the event 14 /// </su Mmary> 15 // <param name = "sender"> </param> 16 // <param name = "e"> </param> 17 private void App_OnDispatcherUnhandledException (object sender, dispatcherUnhandledExceptionEventArgs e) 18 {19 try20 {21 LogHelper. instance. logger. error (e. exception, "UI thread global Exception"); 22 e. handled = true; 23} 24 catch (Exception ex) 25 {26 LogHelper. instance. logger. error (ex, "Global exceptions of irrecoverable UI Threads"); 27 MessageBox. show ("the application is about to exit because of an unrecoverable exception! "); 28} 29} 30 31 /// <summary> 32 // non-UI thread throws global exception event processing 33 /// </summary> 34 // <param name = "sender"> </param> 35 // <param name = "e"> </param> 36 private void CurrentDomain_UnhandledException (object sender, unhandledExceptionEventArgs e) 37 {38 try39 {40 var exception = e. exceptionObject as Exception; 41 if (exception! = Null) 42 {43 LogHelper. instance. logger. error (exception, "non-UI thread global Exception"); 44} 45} 46 catch (exception ex) 47 {48 LogHelper. instance. logger. error (ex, "unrecoverable non-UI thread global exception"); 49 MessageBox. show ("the application is about to exit because of an unrecoverable exception! "); 50} 51}