Unable to catch UnhandledExceptions

I am experiencing really annoying problems trying to handle all unhandled exceptions in a Winforms application.

    private static void Main()

        Application.ThreadException += ApplicationOnThreadException;
        AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
        TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;

        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, false);

        CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
        Thread.CurrentThread.Name = "Main";


            new App().Run(new[] { "" });
        catch (Exception e)

The unhandled exception handler does the obvious few things, log, message, quit.

        Logger.Fatal("Unhandled exception occured : ", ex);
        MessageBox.Show("The application suffered from an unexpected error and needs to close.\nError details:\n\n" + ex.Message,
                "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

When I started I only subscribed to Application.ThreadException and AppDomain.CurrentDomain.UnhandledException. Along the way I added:

  • The [HandleProcessCorruptedStateExceptions]
  • Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, false);
  • a try/catch block surrounding the new App.Run.
The problem

When I start the application (built in release mode) via command scripts (command script sets some environment variables, and starts the application), and then an exception is thrown which is not caught in a try catch block, I get the default unhandled exception dialog.

In this case I just throw an exception in a Task to reproduce the problem easier, I am not looking for help to get rid of the exception :).

The answer I am looking for, is how is it possible to STILL get the dialog while I have set everything to HANDLE to unhandled exception?!

All the steps I've taken so far are ideas I get from other questions on SO, but nothing seems to help.


Apologies as I was pulled into an another meeting but wanted to post the question in the hope for an answer.

@Hans Passant: I tried all three options (Automatic, Catch, Throw). It all results in the exact same behavior... Thanks for your comment though, because it is not what I expected after reading the documentation on MSDN. I would expect that CatchException would tell the unhandled exception handler to actually catch the exception. But anyway, why are my handlers refusing the do their job?


When the application runs (on the UI thread, name "Main") I throw an Exception in a task, to mimic the behavior for testing purposes. In this case, on a button event handler (async)

private async void tsbNew_Click(object sender, EventArgs e)
   // left out irrelevant part

   await Task.Factory.StartNew(() =>
        throw new NullReferenceException();

When the task is collected by the garbage collector, this exception is thrown is unhandled exception, but for (to me) unknown reasons the unhandled exception does NOT get caught by my unhandled exception handlers.


Although quite some time has passed, maybe some googlers might find this answer in the provided link useful: If code never observes a Task’s exception, then when the Task goes away, the TaskScheduler.UnobservedTaskException gets raised, giving the application one more opportunity to “observe” the exception. And if the exception still remains unobserved, the exception escalation policy is then enabled by the exception going unhandled on the finalizer thread. [And an exception thrown on the finalizer threads kills the process without any opportunaty to intercept and handle this exception]. See Unable to catch unhandled exceptions.

Need Your Help

Printing list items as integer

python list python-3.x

So I have some code that reads a file that contains score in this format:

UIWebView loaded from another class, where to put delegate methods?

iphone objective-c delegates

I'm loading in a UIWebView from another class, and that works great.