Http://www.cnblogs.com/liuyueyu/p/4476151.html
Hundred dense a sparse bug is difficult to avoid, no one can guarantee that my program is always 0 bug;
Suddenly take over a very large project, during the operation of the project will be inexplicable abnormal end, extremely difficult to reproduce, and can not find the bug code, too much code.
This time you need to put the bug in the program to record, know where the bug, only good next update to solve.
Add all the code to try catch? It's getting pregnant, it's really big.
Uncaught Exception Handling:
One: C # provides a dispatcherunhandledexception event in the application class to handle uncaught exceptions on the UI thread
public partial class app:application{ protected override void Onstartup (StartupEventArgs e) { Dispatcherunhandledexception + = app_dispatcherunhandledexception; Base. Onstartup (e); } void App_dispatcherunhandledexception (object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { //e.exception An exception occurred //e. Whether the Handled has handled the exception event }}
However, Dispatcherunhandledexception cannot capture exceptions that occur on non-UI threads
II: C # provides the UnhandledException event in the AppDomain class for capturing exceptions that occur in the application domain
AppDomain.CurrentDomain.UnhandledException + = Currentdomain_unhandledexception;void currentdomain_ UnhandledException (object sender, UnhandledExceptionEventArgs e) { MessageBox.Show ("Unhandle");// MessageBox.Show ((E.exceptionobject = = null?) "Null": E.exceptionobject.gettype (). Name), // "currentdomain_unhandledexception:");}
This captures exceptions that occur in the application domain, including UI threads and non-UI thread exceptions, if you have the need to create an application domain, create a complete, and then bind the event.
However, the Appdomain.unhandledexception event did not provide a terminating exception, that is, the exception occurred, you did not capture, notice here, but the end of the program to end.
Special mention is made here of the official package of Delegate.begininvoke ();
"Known:" An exception occurred in the Async function and was not processed:
"Solve:"
1. The Delegate.endinvoke () function is not called:
Terminates execution. The program does not hang out, and the exception is discarded. This is a very hidden bug, it is difficult to find.
The Appdomain.unhandledexception event does not notify you that an exception has occurred, so pay special attention here.
public void Hello () {World (); Load ();} public void World () {
throw new Exception ();
}public void Load () { MessageBox.Show ("Suc");} var action = new Action (Hello); BeginInvoke (null, NULL);
Like this example, if an exception occurs in the world () method, the thread terminates immediately, the Load () function is not executed, and the program does not terminate.
A function that is not executed may result in a very serious exception and is difficult to find.
2. Call the Delegate.endinvoke () function:
Exception thrown, the program hangs out.
Appdomain.unhandledexception event notifies you that an exception has occurred
There may be a need here, what if I want to deal with an exception in the Appdomain.unhandledexception event? The program hangs up too disgusting.
Let's continue to expand the framework.
A small way to troubleshoot accessing UI exceptions in non-UI threads
<summary>///delegate Extension class///</summary>public static class delegateextension{
<summary>/// asynchronous call to catch exceptions///</summary>// <param name= "Dele" ></param> Public static void Unsafebegininvoke (this Delegate dele) { var action = new Action (() = { dele. DynamicInvoke (); }); Action. BeginInvoke (New AsyncCallback (AR) = { Try { action. EndInvoke (AR); } catch (Exception ex) { App.Current.Dispatcher.BeginInvoke (new Action () = { throw ex; })); }} ), null);} }
It's a little nondescript, huh, uh. Throws an exception to the UI thread, letting the Application.dispatcherunhandledexception event handle
Other: C # provides a unobservedtaskexception event in the TaskScheduler class for capturing a Task asynchronous exception
Beg your comment, ^v^, please.
The WPF program handles uncaught exceptions, and the program stops saying goodbye