Unhandled and Thread Exception C# with Example



Unhandled and Thread Exception C# with Example

AppDomain.UnhandledException This event provides notification of uncaught exceptions.It allows the application 
to log information about the exception before the system default handler reports the exception to the user and 
terminates the application.If sufficient information about the state of the application is available, other actions may 
be undertaken — such as saving program data for later recovery.Caution is advised, because program data can 
become corrupted when exceptions are not handled. 
///  
/// The main entry point for the application. 
///  
[STAThread] 
private static void Main(string[] args) 
{ 
AppDomain.CurrentDomain.UnhandledException += new 
UnhandledExceptionEventHandler(UnhandledException); 
} 
Application.ThreadException This event allows your Windows Forms application to handle otherwise unhandled 
exceptions that occur in Windows Forms threads. Attach your event handlers to the ThreadException event to deal 
with these exceptions, which will leave your application in an unknown state. Where possible, exceptions should be 
handled by a structured exception handling block. 
///  
/// The main entry point for the application. 
///  
[STAThread] 
private static void Main(string[] args) 
{ 
AppDomain.CurrentDomain.UnhandledException += new 
UnhandledExceptionEventHandler(UnhandledException); 
Application.ThreadException += new ThreadExceptionEventHandler(ThreadException); 
} 
And finally exception handling 
static void UnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
Exception ex = (Exception)e.ExceptionObject; 
// your code 
} 
 

static void ThreadException(object sender, ThreadExceptionEventArgs e) 
{ 
Exception ex = e.Exception; 
// your code 
} 

0 Comment's

Comment Form

Submit Comment