UnhandledExceptionMode

Hello,

Is there a way to write a custom exception handler, other then the one seen using:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

I want that the application will continue to run, after my exception handler is called with the thrown exception. (like the default win forms exception handler - after the exception form is shown the application continues)

Thanks.




Answer this question

UnhandledExceptionMode

  • Ranmal

    I want my application to continue running. Just like the default exception handler that shows a form with the exception and allows to contiune.

    I'm not sure if what you wrote is what I need..



  • JamesCox1968

    Ori N wrote:

    I want my application to continue running. Just like the default exception handler that shows a form with the exception and allows to contiune.

    I'm not sure if what you wrote is what I need..

    I strongly disagree! You should not continue running because you can't garantee the correctnes of the logic and data in your application. It could be that data isn't commited of that a thread is aborted that is critical for the application. You should log the exception and send an error report to the development team so you can solve the bug! Continue running will not solve any problems and will only get you in uncontrolled situations.

    But as answer to your question, you can just remove the Application.Exit and your application will continue. There is one exception, it could be that the exception was to heavy that your application can't continue, if so when the AppDomain.UnhandledException event is raised with in the argument the UnhandledExceptionEventArgs.IsTerminating set to true.



  • chiomike20000

    Hi Ori,

    as MSDN states you cannot do that. Besides technical reasons of the framework which might make this limitation neccessary there is also the point that swallowing exceptions might leave your app in an inconsistent state where it might be much safer to end and restart the app than leave it running.

    Why do you feel the need to swallow exceptions Are you not in control of the code throwing exceptions Can't you handle them where they occur

    --
    SvenC


  • Elango311325

    You can use the Application.ThreadException event and AppDomain.UnhandledException event.

    First you need to wire the events before your application starts:


    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler( Application_ThreadException );
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( CurrentDomain_UnhandledException );
    // ...
    }

    Now you will trap all unhandled exceptions, whenever a exception was not handled one of these events are called. If the exception was thrown within the application, the Application.ThreadException will be raised, and if not, the AppDomain.UnhandledException will be raised. For the full scope of these event I referrer to documentation.

    Here is a little sample to make it easier to understand:


    /// <summary>
    /// Handles the ThreadException event of the Application control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Threading.ThreadExceptionEventArgs"/> instance containing the event data.</param>
    private static void Application_ThreadException( object sender, ThreadExceptionEventArgs e )
    {
    try
    {
    _log.Error( "ThreadException caught.", e.Exception );
    _log.Info( "Showing error dialog..." );
    using( ErrorForm form = new ErrorForm( e.Exception.ToString() ) )
    {
    Form mainForm = Application.OpenForms["MainForm"];
    if( mainForm == null )
    {
    _log.Warn( "Could not find mainform for showing error dialog." );
    form.ShowDialog();
    }
    else
    {
    if( mainForm.InvokeRequired )
    {
    ShowDialogMethod method = new ShowDialogMethod( form.ShowDialog );
    mainForm.Invoke( method, mainForm );
    }
    else
    {
    form.ShowDialog( mainForm );
    }
    }
    }
    }
    catch( Exception caught )
    {
    // Spit and eat up exception.
    _log.Error( "Error while showing Error Form.", caught );
    }
    finally
    {
    _log.Info( "Terminating..." );
    Application.Exit();
    }
    }
    /// <summary>
    /// Handles the UnhandledException event of the CurrentDomain control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.UnhandledExceptionEventArgs"/> instance containing the event data.</param>
    private static void CurrentDomain_UnhandledException( object sender, UnhandledExceptionEventArgs e )
    {
    Exception exception = (Exception)e.ExceptionObject;
    using( ErrorForm form = new ErrorForm( exception.ToString() ) )
    {
    Form mainForm = Application.OpenForms["MainForm"];
    if( mainForm == null )
    {
    form.ShowDialog();
    }
    else
    {
    MethodInvoker method = delegate
    {
    form.ShowDialog( mainForm );
    };
    mainForm.Invoke( method );
    }
    }
    Application.Exit();
    }

    Please feel free to drop a message whenever you need more information!



  • SPWilkinson

    My application is a bit complex. The user might insert wrong configuration and I can't control all cases. That's why if there is an exception I want to write it to a console window.



  • UnhandledExceptionMode