C # unhandled exception handling

Source: Internet
Author: User
To enhance the robustness of the currently developing system, you need to capture unexpected and unhandled exceptions during the runtime. After checking the information, find The basic steps for the application. threadexception event to handle these exceptions include,
1. Add a function handle to handle the threadexception event.
2. Define a function to handle exceptions
Example:
 [stathread]  
static void main () {application. threadexception + = New threadexceptioneventhandler (application_threadexception); application. run ( New frmmain ();} private static void application_threadexception ( Object sender, threadexceptioneventargs e) {MessageBox. show ( "unhandled exception:" + E. exception. tostring () ;}

This method is easy to use and has a high processing efficiency. You can easily add other functions to handle exceptions based on your intentions. However, I found that if a third-party control does not work at all, the third-party control may have run in different threads. As mentioned in Microsoft's help, the above method can only handle exceptions not handled in the main thread. Okay, check the Internet, find the following method, and use Appdomain. unhandledexception replaces application. threadexception.
First, you need to understand that the event handler function defined at this time needs to be executed in the thread that throws an exception, but the exception prompt in the main thread is completed in the main thread, how can this problem be solved? The followingCodeA complete solution is provided.
 

 Private   Delegate   Void Exceptiondelegate (exception X );

Static Private Frmmain _ mainform;

/// <Summary>
/// The main entry point for the application.
/// </Summary>
[Stathread]
Static Void Main ()
{
_ Mainform = New Frmmain ();
Appdomain. currentdomain. unhandledexception + = New Unhandledexceptioneventhandler (appdomain_unhandledexception );
Application. Run (_ mainform );
}

Private Static Void Appdomain_unhandledexception ( Object Sender, unhandledexceptioneventargs E)
{
Exception exception;

Exception = E. exceptionobject As Exception;
If (Exception =Null )
{
// This is an unmanaged exception, you may want to handle it differently
Return ;
}
Publishonmainthread (exception );
}

Private Static Void Publishonmainthread (exception)
{
If (_ Mainform. invokerequired)
{
// Invoke executes a delegate on the thread that owns _ mainforms's underlying window handle.
_ Mainform. Invoke ( New Exceptiondelegate (handleexception ), New Object [] {Exception });
}
Else
{
Handleexception (exception );
}
}

Private Static Void Handleexception (exception)
{
If (Systeminformation. userinteractive)
{
Using (Threadexceptiondialog dialog = New Threadexceptiondialog (exception ))
{
If (Dialog. showdialog () = dialogresult. Cancel)
Return ;
}
Application. Exit ();
Environment. Exit (0 );
}
}

Private Void Threadmethod ()
{
Throw New Exception ( "From New thread" );
}

Private Void Button#click ( Object Sender, system. eventargs E)
{
Thread thread;
Thread = New Thread ( New Threadstart (threadmethod ));
Thread. Start ();
}

Note that:
1. All AppdomainOf UnhandledexceptionAdd a processing
2, The unhandledexceptioneventargs parameter contains Isterminating attribute, indicating whether to abort Common Language Runtime

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.