C # capture global exceptions using winform

Source: Internet
Author: User
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 >}

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.