3

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?

Rans
  • 569
  • 7
  • 14
  • Because you're not waiting for the `Task`. Exception lost in Thread because there's no way to return it into the main thread. For test, add `Initialization.GetAwaiter().GetResult()` to the constructor. – aepot May 03 '20 at 15:07
  • This creates a UI freeze and it doesn't work – Rans May 03 '20 at 15:15
  • I know about freeze, that was for exception test. `Task.Run(async () => await InitializeAsync(language))` maybe then? Or simply `Task.Run(() => InitializeAsync(language))` – aepot May 03 '20 at 15:23
  • That didn't work either and that can lead to deadlock :) – Rans May 03 '20 at 15:26
  • Change `MessageBox.Show` to for example to `Debug.Writeline` to see the fact of handler call or insert a beakpoint there. MessageBox can cause problems when called from different from main UI thread. – aepot May 03 '20 at 15:36
  • And i don't see `base.OnStartup()` call in the overriden method. Is it by design? – aepot May 03 '20 at 15:38
  • I have inserted a breakpoint, it doesn't stop there. And `base.OnStartup(e);` is also there, I only deleted it here on SO – Rans May 03 '20 at 15:44
  • What kind of app is it? I'll try to reproduce. – aepot May 03 '20 at 15:54
  • 1
    WPF .net framework – Rans May 03 '20 at 15:56
  • I've found the source of the question: `AppDomain.CurrentDomain.UnhandledException` being fired only on App crash, the app stops it's work then. Regarding to [this article](https://learn.microsoft.com/en-us/dotnet/standard/threading/exceptions-in-managed-threads?view=netcore-3.1) Exceptions in Threads doesn't cause the application crash but only the thread abort. If you'll tell me what exacly do you want to achieve, I'll write some answer for it. – aepot May 03 '20 at 17:46
  • 1
    What I want to do is, if any unhandled exception occurs in the program, can be caught in a central location in the code like `UnhandledException`. – Rans May 03 '20 at 17:50
  • Unfortunately that handler cannot receive an exception if other parts of .NET Framerwork handle that exception earlier. Your code must also register to other events, like `TaskScheduler.UnobservedTaskException` and `Application.ThreadException`. There are several of them. – Lex Li May 03 '20 at 23:53
  • @LexLi `UnobservedTaskException` did not work for unfortunately either – Rans May 04 '20 at 20:20
  • And what exactly do you mean by "that handler cannot receive an exception if other parts of .NET Framerwork handle that exception earlier"? the exception is never handled in the code – Rans May 04 '20 at 20:22
  • How about [this SO answer](https://stackoverflow.com/a/22395161/3791245)? Key point: exception remains dormant (meaning not thrown) until the Task is waited or awaited. So can you post where in your sample code you actually wait or await on your Task? If you don't, it won't get thrown. – Sean Skelly Jun 27 '20 at 00:24

0 Answers0