Tuesday, April 2, 2013

Handling Unhandled Exceptions in WinForms

Hello Friends!
 
A truth wherein Exceptions do occur in our code. As an expert developer we need to provide appropriate exception handling mechanism for our application. But there will always be a situation where we might have forgotten to add exception handling block or we may have not predicted exceptions.

 
So, when unhandled exception occurs our application gets stopped and user will be left no where.
.NET development provides a way to handle unhandled exceptions.

 
We can handle such exceptions by following ways : 


 1) Application.ThreadException Event : It allows to handle unhandled exceptions. This event is fired when exception is thrown from code that was ultimately called as a windows message. It does not handled exceptions thrown by thread. 

 2) AppDomain.CurrentDomain.UnhandledException Event : This event fires when there is an unhandled exception in any thread. However It provides a good way to handle exceptions , It does not stop application from shutting down. 

 3) Try-catch block arounf the entry point of form : This will make sure that all the exceptions which are not handled by above events are going to be handled here.
Lets have a look at code :
We need to register event handlers for above described events in Main method.
static void Main()
{
try
{

// Set the unhandled exception mode to force all Windows Forms errors to go through
// our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);


// Adding the event handler for handling UI thread exceptions to the event.
System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmTestHandler());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Environment.Exit(1);
}
}


// Generating an exception .
private void btnThrowUIException_Click(object sender, EventArgs e)
{
throw new ArgumentException("Invalid parameters.");
}

// Generating Thread Exception
private void btnThreadException_Click(object sender, EventArgs e)
{
ThreadStart newThreadStart = new ThreadStart(ExecuteThread);
Thread newThread = new Thread(newThreadStart);
newThread.Start();
}

// Handling Application level exception.
public static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
try
{
MessageBox.Show(e.Exception.Message);
}
catch (Exception ex)
{
try
{
MessageBox.Show("Unhandled exception has occured.Application is going to be shutdowm.");
}
finally
{
Environment.Exit(1);
}
}
}

// Handling Unhandled Thread Exception.
public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{

try
{
MessageBox.Show("Unhandled exception has occured.");
}
finally
{
Application.Exit();
Environment.Exit(1);
}
}
finally
{
Application.Exit();
Environment.Exit(1);
} 
}

This way we can handle all unhandled exceptions in our application.
We can log all information about exception in trace file or event log file.
Hope this blog will be useful to all.

No comments:

Post a Comment

Software Development Blogs - BlogCatalog Blog Directory RSS Search Technology Blogs - Blog Rankings Blog Directory