To handle unhandled exception

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.