Click here to Skip to main content
12,294,775 members (68,788 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

10.4K views
5 bookmarked
Posted

Recovering your application from unhandled exception in .NET

, 10 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Recovering your application from unhandled exception in .NET

Exception handling is one of the nightmares and the best practice for any programmer. But doesn't matter how good the programmer is, there will be always a chance of unhandled exception daunting the users. .NET 2.0+ however has a better option of handling it which is focused on recovering your application on the next start.

AppDomain.UnhandledException is an event which will allow you to handle any unhandled exceptions that would occur anywhere on your application. When you define the delegate for the above said event, it will allow you to write the code to save the current state of the application or do any custom operations so that the crash can be recovered during next recovery. However, if you wish to give the user the control of handling the situation, it can be done using a exception dialog.

Now let's get into business.

/// 
/// The main entry point for the application.
/// 
[STAThread]
static void Main()
{
    AppDomain.CurrentDomain.UnhandledException +=
         new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    Application.SetUnhandledExceptionMode(
        UnhandledExceptionMode.CatchException, true);
     Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    // Check for application crash and recover accordingly here.
    Application.Run(new Form1());
}

/// 
/// Handles the UnhandledException event of the CurrentDomain control.
/// 
/// <span class="code-SummaryComment"><param name="sender">The source of the event.
</span>

If you are looking into the handling of unexpected error, you can opt for not giving the user an option to choose what the application has to do and simply log the application state and close the application and while the user starts the next time, you can recover it. This is similar to the Microsoft Word approach.

Or you can opt for the user to choose whether to continue or quit the application after logging the details using an exception dialog. This can be used where the application is not processing any critical data and can survive the crash.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jebarson
Software Developer (Senior) Microsoft Corporation
India India
I work for Microsoft on MS technologies for application development. My interests include .net, WCF, Azure, Windows Phone, ASP.net, SL, WCF, WPF and many more.

You can visit my site at http://www.jebarson.info

Follow me on twitter @jebarson007

You may also be interested in...

Comments and Discussions

 
Generalnot in multithreaded applications Pin
ivanicin18-Jan-11 11:44
memberivanicin18-Jan-11 11:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 10 Jan 2011
Article Copyright 2011 by jebarson
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid