To capture those we do notTry {...} Catch {...}To the exception (Unhandled exception), And there isUIGive users a friendly reminder that we need a mechanism to handle these exceptions. Besides, if we useTry {...} Catch {...}It will be a thankless task.
. NetSome mechanisms are provided for us to handleUnhandled exception:
ASP. NET & Web Services
Please refer toDetails
Windows Forms & Console
MsdnAbove usageThe unhandledexceptioneventhandler delegateBut I found that it is not easy to use.
MyCodeAs follows:
[Stathread]
Private Static Void Main ( String [] ARGs)
{< br> // error handle
appdomain. currentdomain. unhandledexception += New unhandledexceptioneventhandler (currentdomain_unhandledexception );
application. run ( New mainform ());
}
Private Static Void Currentdomain_unhandledexception ( Object Sender, unhandledexceptioneventargs E)
{
//Show a UI to user... For example:
MessageBox. Show ("error ...");
}
This code is available in Debug Mode is lucky (click the pop-up MessageBox Of OK Button, Application End), but in Release Mode does not work at all-the frightening JIT debugging Dialog box. For more information, see CLR exceptions pm. . The following code was used later: Application. threadexception + = New Threadexceptioneventhandler (application_threadexception );
Private Static Void Application_threadexception ( Object Sender, threadexceptioneventargs E)
{
//...
}
After the test, we found thatUnhandled exception. However, note that this method can only capture exceptions on the main thread.
Reference
User Friendly Exception Handling
Managing unhandled exceptions in. net