http://www.developer.com/net/cplus/article.php/3348741/C-Tip-Using-Custom-Exception-Handlers-to-Restart-Your-Application.htm
Welcome to this week's installment of .NET Tips & Techniques! Each week, award-winning Architect and Lead Programmer Tom Archer demonstrates how to perform a practical .NET programming task using either C# or Managed C++ Extensions. When using the .NET Framework, you will implement try/catch blocks to handle specific exceptions that your application expects. However, one often-overlooked ability of .NET is that you can create a custom exception handler that will allow you to catch all unhandled exceptions thrown during the execution of your application. This allows you to terminate your application in a controlled manner and perform any needed application-specific cleanup and error logging. This feature came in handy for me recently when a client requested that their application log any unhandled exceptions and automatically restart itself. In this week's installment to the C# Tips and Techniques series, I'll illustrate how .NET makes this a snap to implement.
C++ Tip: Using Custom Exception Handlers to Restart Your Application
May 3, 2004
using System.Threading;
// Custom Exception class to catch all "unhandled exceptions"
public class CustomExceptionHandler
{
// Event handler that will be called when an unhandled
// exception is caught
public void OnThreadException(object sender,
ThreadExceptionEventArgs t)
{
// Log the exception to a file
LogException(t.Exception);
// Tell the user that the app will restart
MessageBox.Show("A Fatal Error was detected and logged.
Click the OK button to restart the
application",
"Fatal Application Error",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
// Shut down the current app instance
Application.Exit();
// Restart the app
System.Diagnostics.Process.Start(Application.ExecutablePath);
}
// *Very* simple logging function to write exception details
// to disk
protected void LogException(Exception e)
{
DateTime now = System.DateTime.Now;
string error = e.Message + "\n\nStack Trace:\n"
+ e.StackTrace;
string filename = String.Format("Log-{0}{1}{2}-{3}{4}
{5}.txt",
now.Year.ToString(),
now.Month.ToString(),
now.Day.ToString(),
now.Hour, now.Minute,
now.Second);
StreamWriter stream = null;
try
{
stream = new StreamWriter(filename, false);
stream.Write(error);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (null != stream)
stream.Close();
}
}
};
CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new System.Threading.
ThreadExceptionEventHandler(eh.
OnThreadException);

private void btnHandledException_Click(object sender,
System.EventArgs e)
{
try
{
throw new Exception("Handled Exception");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnUnhandledException_Click(object sender,
System.EventArgs e)
{
throw new Exception("Unhandled Exception!!");
}

Download the Code
To download the accompanying source code for this tip, click here.
About the Author
The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.