1
votes

I was reading C# in a nutshell (Threading chapter) and was trying to catch a divide by zero exception that I throw in a new thread from a button event handler. The AppDomain.CurrentDomain.UnhandledException fires as expected. The event handling methods simply show a window called "ErrorWindow". When I step into the constructor of the window, the debugger throw the exception back and the window doesn't show.

Here's what I have:

  1. Target Framework: 4.5.2
  2. Target Platform: X64

The application is a WPF windows application and contains two windows:

  1. MainWindow: Has a button that executes the code snippet below.
  2. ErrorWindow: Just a blank window to show in the AppDomain.CurrentDomain.UnhandledException event handler

Code snippet:

var thread = new Thread(() =>
{
int result = 100 / int.Parse("0");
});
thread.Start();

I'm attaching the AppDomain.CurrentDomain.UnhandledException event handler in a StartUp event handler in App.xaml.cs. The event handler simply create a instance of the ErrorWindow and attemtps to show it.

If someone is able to recreate this behavior and explain why, that'd be great. I'll share my VS project if needed.

PS: This only happens when I create a thread. If my DivideByZeroException gets thrown from the main UI thread, then the window shows as expected.

PS: Marshalling the showing of the window to the UI thread doesn't work and I don't expect it to. Thanks!

1
Do you have a special requirement for using Thread? Task makes this scenario much easier, by giving you a supported way to catch and observe the exceptionKevin Gosse
I don't have a requirement. I prefer to use thread because I can. Thank you @KevinGosse!Amen Jlili

1 Answers

0
votes

IMHO the AppDomain.UnhandledException Event is not meant to show windows or error dialogs. As reported in the Remarks section:

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application. If sufficient information about the state of the application is available, other actions may be undertaken — such as saving program data for later recovery. Caution is advised, because program data can become corrupted when exceptions are not handled.

Instead, if you want to trap an exception and handle it, you can use the Application.DispatcherUnhandledException Event. In this case you can keep you application alive, just by setting the Handled property to true:

Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;

private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    e.Handled = true;
    /* Show your error window */
}

But pay attention, because:

If an exception is not handled on either a background user interface (UI) thread (a thread with its own Dispatcher) or a background worker thread (a thread without a Dispatcher), the exception is not forwarded to the main UI thread. Consequently, DispatcherUnhandledException is not raised.

I hope it cal help you.