-
Notifications
You must be signed in to change notification settings - Fork 406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UnobservedTaskException does not catch exceptions from async/await code #57
Comments
After some investigation, I found this event handler to do the trick:
I'm not sure if this handler catches all exceptions, or if we need to use |
When you When you Here's a little illustration of what's going on: static async Task Boom()
{
throw new Exception("BOOOM!");
}
static void Main(string[] args)
{
// Bye-bye message
AppDomain.CurrentDomain.UnhandledException += (sender, e) => Console.WriteLine("Aww, we crashed...");
Action synchronous = () => Boom().Wait();
Action asynchronous = async () => await Boom();
// This one prints "Gotcha!"
try { synchronous(); }
catch { Console.WriteLine("Gotcha!"); }
// This one doesn't print "Gotcha!" and crashes the application
try { asynchronous(); }
catch { Console.WriteLine("Gotcha!"); }
Thread.Sleep(Timeout.Infinite);
} But don't worry, fixing your example is rather simple: Schedule(() => {
var faulty= new FaultyComponent().Run(); // Here the faulty task starts to run on its own thread
// Do some other stuff
faulty.Wait(); // Here we join back the faulty task, if any exception were raised you're gonna get it wrapped in an AggregateException
}).ToRunOnceIn(1).Seconds(); |
Here's a naive unit test demonstrating that exceptions happening inside of async/await code is not caught:
The text was updated successfully, but these errors were encountered: