C# Tutorial - Dealing With Unhandled Exceptions
Generally, since we are in the managed world of .NET and C#, when something horrible goes wrong with your program, it will eventually manifest itself as an unhandled exception - and then the app will crash and burn. Now, I suppose you could try and surround all your code with generic try-catch statements, but (I'm hoping) we all know that that is just a horrible idea, from performance, maintenance, and code readability perspectives. Fortunately, there is a better way to implement that idea in .NET - the UnhandledException event. |
The
UnhandledException is an event on the AppDomain class. An AppDomain is, in very simplistic terms, the execution environment for your code. We are going to assume today that there is only one AppDomain, and we aren't going to worry about it any more than that. Talking about AppDomains could probably take an entire tutorial in and of itself. But anyway, we have this event - what does it do and how do we attach to it?What it does is pretty simple - the event is fired every time there is an unhandled exception that propagates all the way to the top of your application. Now, if there was nothing attached to the event, the app would show one of those classic "this app has encountered a problem and needs to close" dialogs, and down your app would go. But once you attach to this event, you have a chance to do something.
How to attach to the event is a little weird - but only in that you may have never added code to this part of a C# app before - especially if what you mostly work with are GUI apps. We need to add a line to the
Main method (by default in the "Program.cs" file in a new Windows Application):using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace MyApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(
CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
namespace MyApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(
CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
That is the content of a default "Program.cs" file for a new Visual Studio Windows Application called "MyApp" - with one difference. The line of code
AppDomain.CurrentDomain.Unhandled... is what I added in order to attach to the UnhandledException event of my AppDomain. Of course, here it is referencing a function that does not yet exist (CurrentDomain_UnhandledException) - but we will get to that next.Ok, lets write that method:
static void CurrentDomain_UnhandledException
(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show("Whoops! Please contact the developers "
+ "with the following information:\n\n"
+ ex.Message + ex.StackTrace, "Fatal Error",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show("Whoops! Please contact the developers "
+ "with the following information:\n\n"
+ ex.Message + ex.StackTrace, "Fatal Error",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
And there you go - now your unhandled exceptions are being shown in a nice happy dialog! Of course, you could do other things here - like log the exception, or try and soften the blow of the crash by saving whatever program state you can. You can't, however, actually keep the program from crashing - there is no way to "catch" the exception at this point and let the app keep on running.
But wait, theres more! As I stated before, with the
UnhandledException event on AppDomain, any exception in the current AppDomain will trigger that event. This includes not only the main application thread (the UI thread), but any other threads as well. And, as I just mentioned, there is no way to "recover" once you hit that point. However, there is actually a different way to handle unhandled exceptions on the Application thread (i.e., the thread that is handling your UI - forms, painting/ user input, etc..). This is through the ThreadException event on Application. To do this, we add another line to the main method:Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(
Application_ThreadException);
new System.Threading.ThreadExceptionEventHandler(
Application_ThreadException);
And we define the function
Application_ThreadException:public static void Application_ThreadException
(object sender, System.Threading.ThreadExceptionEventArgs e)
{
DialogResult result = DialogResult.Abort;
try
{
result = MessageBox.Show("Whoops! Please contact the "
+ "developers with the following information:\n\n"
+ e.Exception.Message + e.Exception.StackTrace,
"Application Error", MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Stop);
}
finally
{
if (result == DialogResult.Abort)
{
Application.Exit();
}
}
}
(object sender, System.Threading.ThreadExceptionEventArgs e)
{
DialogResult result = DialogResult.Abort;
try
{
result = MessageBox.Show("Whoops! Please contact the "
+ "developers with the following information:\n\n"
+ e.Exception.Message + e.Exception.StackTrace,
"Application Error", MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Stop);
}
finally
{
if (result == DialogResult.Abort)
{
Application.Exit();
}
}
}
With this method hooked to the
Application.ThreadException, unhandled exceptions on the main application thread will not hit the UnhandledException event on the AppDomain - and the app will no longer terminate by default. As you can see in this method, we show a dialog asking if the user wants to continue or not - and if they choose abort, we close the app. Otherwise, we just let the app continue on.Here is what the file "Program.cs" looks like now:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace MyApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(
CurrentDomain_UnhandledException);
Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(
Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void CurrentDomain_UnhandledException
(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show("Whoops! Please contact the developers "
+ "with the following information:\n\n"
+ ex.Message + ex.StackTrace, "Fatal Error",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
public static void Application_ThreadException
(object sender, System.Threading.ThreadExceptionEventArgs e)
{
DialogResult result = DialogResult.Abort;
try
{
result = MessageBox.Show("Whoops! Please contact the "
+ "developers with the following information:\n\n"
+ e.Exception.Message + e.Exception.StackTrace,
"Application Error", MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Stop);
}
finally
{
if (result == DialogResult.Abort)
{
Application.Exit();
}
}
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
namespace MyApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(
CurrentDomain_UnhandledException);
Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(
Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void CurrentDomain_UnhandledException
(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show("Whoops! Please contact the developers "
+ "with the following information:\n\n"
+ ex.Message + ex.StackTrace, "Fatal Error",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
public static void Application_ThreadException
(object sender, System.Threading.ThreadExceptionEventArgs e)
{
DialogResult result = DialogResult.Abort;
try
{
result = MessageBox.Show("Whoops! Please contact the "
+ "developers with the following information:\n\n"
+ e.Exception.Message + e.Exception.StackTrace,
"Application Error", MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Stop);
}
finally
{
if (result == DialogResult.Abort)
{
Application.Exit();
}
}
}
}
}
In any decent sized application, the code in either of these two methods would probably be much more complicated - logging much more state information, trying to determine if the app could continue at all, so on and so forth. But hopefully this gives you an idea of where to put that sort of logic in a C# application. As always, if you have questions, please feel free to leave them in the comments.
Posted in C#, All Tutorials by The Tallest |

September 10th, 2007 at 12:45 pm
Very cool feature -I’m new to C#, but in Java I used to create Aspects to intercept unhandled exceptions and email the developers. This comes for free in C#, I see. Thanks!
November 7th, 2007 at 3:47 am
nice article of nice exit from Application
February 5th, 2008 at 6:23 pm
Works great when run inside the VS2005 IDE but…
…when app is run outside as a standalone .exe, any unhandled expecption still brings up funky “Unhandled exception has occurred in your application…” will Details, Continue, Quit buttons.
I have SEARCHED and SEARCHED and no straight answers on how to avoid this. You seem pretty sharp with exception handling… how can I get around this?
Please email shawn@ignited.tv directly if you need a screenshot… and if you have a solution… MUCH APPRECIATED
March 18th, 2008 at 1:25 am
Shawn, remember to also catch exceptions thrown in Main(), ie:
[STAThread]
static void Main()
{
try
{
}
catch (Exception e)
{
MessageBox.Show();
}
}
September 18th, 2008 at 9:05 am
Hi shawn,
Yes it will bring up the default .NET unhandled exception if you use AppDomain.CurrentDomain.UnhandledException so its better not to use it. Use Application.ThreadException instead as shown later in this article.
To the author: Thanks for nice article BTW!
November 12th, 2008 at 1:09 pm
Calling Environment.Exit() in the UnhandledException handler will quash the Unhandled exception dialog.
December 5th, 2008 at 3:14 am
Tnx very good article