Using system;
Using system. Collections. Generic;
Using system. Windows. forms;
Using system. IO;
Namespace gobalexception
{
Static class Program
{
/// <Summary>
/// Application Program .
/// </Summary>
[Stathread]
Static void main ()
{
Try
{
// Handle uncaptured exceptions
Application. setunhandledexceptionmode (unhandledexceptionmode. catchexception );
// Handle UI thread exceptions
Application. threadexception + = new system. Threading. threadexceptioneventhandler (application_threadexception );
// Handle non-UI thread exceptions
Appdomain. currentdomain. unhandledexception + = new unhandledexceptioneventhandler (currentdomain_unhandledexception );
application. enablevisualstyles ();
application. setcompatibletextrenderingdefault (false);
application. run (New form1 ();
}< br> catch (exception ex)
{< br> string STR = "";
string strdateinfo = "Exceptions not handled by the application:" + datetime. now. tostring () + "\ r \ n";
If (ex! = NULL)
{
STR = string. format (strdateinfo + "exception type: {0} \ r \ n exception message: {1} \ r \ n exception message: {2} \ r \ n ",
Ex. GetType (). Name, Ex. Message, Ex. stacktrace );
}
Else
{
STR = string. Format ("Application Thread error: {0}", ex );
}
Writelog (STR );
MessageBox. Show ("a fatal error occurs. Please contact the author in time! "," System error ", messageboxbuttons. OK, messageboxicon. Error );
}
}
/// <Summary>
/// This is the method for processing the unprocessed exception. I am writing the error details to the text. If an error occurs, a beautiful error prompt form is displayed, for your reference
/// There are many practices, such as recording the error details to the text and database, sending an error email to the author's mailbox, or re-initializing after an error, etc.
/// This is what the benevolent and wise have done.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Static void application_threadexception (Object sender, system. Threading. threadexceptioneventargs E)
{
String STR = "";
String strdateinfo = "exception not handled by the application:" + datetime. Now. tostring () + "\ r \ n ";
Exception error = E. exception as exception;
If (error! = NULL)
{
STR = string. format (strdateinfo + "exception type: {0} \ r \ n exception message: {1} \ r \ n exception message: {2} \ r \ n ",
Error. GetType (). Name, error. Message, error. stacktrace );
}
Else
{
STR = string. Format ("Application Thread error: {0}", e );
}
Writelog (STR );
MessageBox. Show ("a fatal error occurs. Please contact the author in time! "," System error ", messageboxbuttons. OK, messageboxicon. Error );
}
Static void currentdomain_unhandledexception (Object sender, unhandledexceptioneventargs E)
{
String STR = "";
Exception error = E. exceptionobject as exception;
String strdateinfo = "exception not handled by the application:" + datetime. Now. tostring () + "\ r \ n ";
If (error! = NULL)
{
STR = string. Format (strdateinfo + "application unhandledexception: {0}; \ n \ r stack information: {1}", error. Message, error. stacktrace );
}
Else
{
STR = string. Format ("application unhandlederror: {0}", e );
}
Writelog (STR );
MessageBox. Show ("a fatal error occurs. Please stop the current operation and contact the author in time! "," System error ", messageboxbuttons. OK, messageboxicon. Error );
}
/// <Summary>
/// Write an object
/// </Summary>
/// <Param name = "str"> </param>
Static void writelog (string Str)
{
If (! Directory. exists ("errlog "))
{
Directory. createdirectory ("errlog ");
}
using (streamwriter Sw = new streamwriter (@ "errlog \ errlog.txt", true)
{< br> SW. writeline (STR);
SW. writeline ("---------------------------------------------------------");
SW. close ();
}< BR >}