How to handle unhandled exceptions in Windows Forms

Source: Internet
Author: User

This article Reprinted from: http://www.cnblogs.com/yinzixin/

Very useful article, thanks to yinzixin

-----------------------------------

If an uncaptured exception exists in the Windows Forms program, it will cause the program to crash and give the user a bad impression. For example, the following program simulates an uncaptured exception:


Button event:

private void button1_Click(object sender, EventArgs e)
{
throw new Exception();
}

Click Exception. The following default window is displayed.

Windows Forms provides two events to handle uncaptured exceptions, namely Application. threadException and AppDomain. unhandledException event. The former is used to handle exceptions in the UI thread, and the latter is used to handle exceptions in other threads. To enable the program to handle exceptions using custom events, use the following code:

Static class Program
{
/// <Summary>
/// The main entry point for the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. ThreadException + = new System. Threading. ThreadExceptionEventHandler (Application_ThreadException );
AppDomain. CurrentDomain. UnhandledException + = new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException );

Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
Application. Run (new Form1 ());
}

Static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e)
{
MessageBox. Show ("sorry, your operation has not been completed. Please try again or contact the software provider ");
LogUnhandledException (e. ExceptionObject );
}

Static void Application_ThreadException (object sender, System. Threading. ThreadExceptionEventArgs e)
{
MessageBox. Show ("sorry, your operation has not been completed. Please try again or contact the software provider ");
LogUnhandledException (e. Exception );
}

Static void LogUnhandledException (object exceptionobj)
{
// Log the exception here or report it to developer
}
}

The result of running the program is as follows:

 

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.