FROM MSDN:
If the UnhandledException event is handled in the default application domain, it is raised there for any unhandled exception in any thread, no matter what application domain the thread started in.
So I have the following:
An async method in a class:
public Task Initialization { get; }
public MyClass(string language)
{
Initialization = InitializeAsync(language);
}
private async Task InitializeAsync(string language)
{
throw new Exception("Test Exception");
}
Register an event to catch all unhandled exceptions (in the OnStartUp method):
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += MyHandler;
}
private static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
MessageBox.Show("Exception", "Caption");
}
The problem is that UnhandledException event does not catch the exception in the InitializeAsync method
This question here on SO sounds to me that the UnhandledException catches the exception in all threads, but for me not.
I tried to remove ConfigureAwait(false), it doesn't work either.
I used UnobservedTaskException and DispatcherUnhandledException and it doesn't work either.
When I pack the exception in MyClass constructor everything works fine but in the InitializeAsync method it doesn't
Any ideas?