question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

App.UnhandledException doesn't catch exceptions from other threads.

See original GitHub issue

Describe the bug Unable to catch UnhandledException (at app level) when the exception originates from async Task.

Steps to reproduce the bug

  1. Create new WinUI3 project
  2. In App.cs handle UnhandledException:
public App()
        {
            this.InitializeComponent();
            this.UnhandledException += App_UnhandledException;
        }


        private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
        {
            Debug.WriteLine("WinUI App Crashed");
        }
  1. In MainWindow.cs add a button with code-behind that will throw a devide-by-zero exception from an Async method:
private async void myButton_Click(object sender, RoutedEventArgs e)
        {
            int t = await Task.Run(() =>
            {
                var x = 0;
                var y = 3 / x;
                return y;
            });
        }

Expected behavior Would expect the App_UnhandledException to ‘handle’ this exception.

Screenshots image

Version Info WinUI 0.8.0-preview Windows 10

NuGet package version: Microsoft.ProjectReunion (0.8.0-preview) Microsoft.ProjectReunion.Foundation (0.8.0-preview) Microsoft.ProjectReunion.WinUI (0.8.0-preview)

Windows app type:

UWP Win32
No Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
October 2020 Update (19042) Yes
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context GitHub example with 2 ‘identical’ projects, one UWP (WinUI2) and one WinUI3, to show the difference in behavior between the two. https://github.com/artkat/CrashingApp

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:31 (12 by maintainers)

github_iconTop GitHub Comments

12reactions
artkatcommented, May 26, 2022

This bug has been reported nearly a year ago. Still, no one has been assigned to fix it. I’m disappointed, frustrated, annoyed, and honestly quite pissed that this is the way Microsoft handles this project.

11reactions
Scottj1scommented, Sep 6, 2022

App.UnhandledException fix will be available in Windows App SDK 1.2 Preview 2

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Catching unhandled exception on separate threads
I am using the following event to catch unhandled exceptions in the main UI thread. ... However, this seems to shut down the...
Read more >
Exceptions in Managed Threads
See how unhandled exceptions are handled in .NET. Most unhandled thread exceptions proceed naturally and lead to application termination.
Read more >
Application.ThreadException Event (System.Windows.Forms)
To catch exceptions that occur in threads not created and owned by Windows Forms, use the UnhandledException event handler. To guarantee that no...
Read more >
Unhandled exceptions terminate the thread (not the process)
If an unhandled exception condition happens in a secondary thread and moves all the way to the first invocation in the thread without...
Read more >
15.3. Preventing Silent Thread Termination - C# Cookbook ...
An unhandled exception in a spawned worker thread, however, will terminate only that thread. This will happen without any visible warnings, and your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found