There are always some exceptions that we could not expect when writing code. What we need to do is to record them.
The following is a sample code:
// Catch the current unbound exception
AppDomain. CurrentDomain. UnhandledException + = new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException );
// Capture thread exceptions
Application. ThreadException + = new ThreadExceptionEventHandler (Application_ThreadException );
Static void Application_ThreadException (object sender, ThreadExceptionEventArgs e)
{
Exception exp = e. Exception;
// Todo: The record is abnormal.
}
Static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e)
{
Exception exp = (Exception) e. ExceptionObject;
// Todo: The record is abnormal.
}
It should be noted that the exception will be thrown, and the program will crash.