Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching exception in Main() method

Consider the following simple application: a windows form created by a "new C# windows application" sequence in VS that was modified in a following way:

public static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    try
    {
        Application.Run(new Form1());
    }
    catch (Exception ex)
    {
        MessageBox.Show("An unexpected exception was caught.");
    }

}

Form1.cs contains the following modifications:

private void Form1_Load(object sender, EventArgs e)
{
    throw new Exception("Error");

}

If I press F5 in IDE, then, as I expect, I see a message box saying that exception was caught and the application quits.

If I go to Debug(or Release)/bin and launch the executable, I see the standard "Unhandled exception" window, meaning that my exception handler doesn't work.

Obviously, that has something to do with exception being thrown from a different thread that Application.Run is called from. But the question remains - why the behavior differs depending on whether the application has been run from IDE or from command line? What is the best practice to ensure that no exceptions remain unhandled in the application?

like image 852
Corvin Avatar asked Apr 27 '10 11:04

Corvin


1 Answers

Normally Application.ThreadException will handle the exception in the Load event. You'll get the ThreadExceptionDialog that offers the Quit and Continue options.

But not when a debugger is attached. The catch clause in the message loop that displays the dialog is intentionally disabled in that case. That's necessary because it would be very difficult to trouble-shoot exceptions if that dialog pops up when you debug a program. Which this catcher no longer active, your catch clause in the Main() method now gets a shot at the exception.

You can make it consistent by using Application.SetUnhandledExceptionMode() in the Main() method. You shouldn't, exceptions really are hard to debug if you do this. If you want to customize exception handling for the UI thread then you should register your own Application.ThreadException handler:

  if (!System.Diagnostics.Debugger.IsAttached)
    Application.ThreadException += myThreadException;

Trapping unhandled exceptions in worker threads requires a AppDomain.UnhandledException handler. They are not recoverable.

Also beware of a bug in 64-bit Windows, exceptions in the Load event are swallowed without diagnostic when a debugger is attached. Force AnyCPU mode to avoid that trap.

like image 115
Hans Passant Avatar answered Sep 30 '22 05:09

Hans Passant