microsoft-ui-xaml: App.UnhandledException doesn't catch exceptions from other threads.
Describe the bug Unable to catch UnhandledException (at app level) when the exception originates from async Task.
Steps to reproduce the bug
- Create new WinUI3 project
- 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");
}
- 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

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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 6
- Comments: 40 (12 by maintainers)
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.
App.UnhandledException fix will be available in Windows App SDK 1.2 Preview 2
Really need a neat way to be able to catch unhandled exceptions, for a few reasons:
Nothing is more jarring than just having an app disappear.
@k563 The repro project was very helpful and reminded me of several loose ends I neglected to tie up. I’ve captured these in several new issues: App_UnhandledException’s UnhandledExceptionEventArgs.Exception can only be fetched once - should be cached Do_Abi_* event/callback handlers should wrap invocations with RoReportUnhandledError When debugging, an exception-invoked FailFast does not terminate, but allows the app to continue in a bad state
Please note the workarounds for the first two issues, which should largely unblock you until these are resolved.
Please can anyone work on this issue? I cannot trace the bug with this exception. Or at least, we should have a workaround to catch this exception we cannot let the app crash in the end user.
I’m aware it’s a WinUI issue. But MAUI hasn’t been great all around. So for our next app, we’ll probably look at choices outside the .NET ecosystem
Copying this in here as the other issue is closed – Here’s another version, and as minimal as one can imagine. It is synchronous, so, the problem is not only in async methods.
Use the “Blank App, Packaged (WinUI 3 in Desktop)” template with the Visual Studio configuration copied below. Edit the OnLaunched method in App.xaml.cs as follows:
Expected: App_UnhandledException is invoked.
Actual: App_UnhandledException is not invoked, and the program crashes with the message “An unhandled Win32 exception occurred in [15028] Test.exe” in a window that asks you to attach a debugger.
– Microsoft Visual Studio Community 2022 Version 17.2.0 VisualStudio.17.Release/17.2.0+32505.173 Microsoft .NET Framework Version 4.8.04084
Installed Version: Community
Visual C++ 2022 00482-90000-00000-AA995 Microsoft Visual C++ 2022
ASP.NET and Web Tools 2019 17.2.388.21846 ASP.NET and Web Tools 2019
Azure App Service Tools v3.0.0 17.2.388.21846 Azure App Service Tools v3.0.0
C# Tools 4.2.0-4.22252.24+47cdc16a21bbb8a4aadfb666b011e2059e1be5d2 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.
Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.
Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines
NuGet Package Manager 6.2.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/
TypeScript Tools 17.0.10418.2001 TypeScript Tools for Microsoft Visual Studio
Visual Basic Tools 4.2.0-4.22252.24+47cdc16a21bbb8a4aadfb666b011e2059e1be5d2 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.
Visual F# Tools 17.1.0-beta.22178.3+6da0245a7ce4bb8483b8d1f2993c8ecaea967ad9 Microsoft Visual F# Tools
Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio.
Issue has been fixed internally and will be included in v1.3
🎉
Microsoft.WindowsAppSDK v1.2.220930.4-preview2has been released which ~fixes* this issue.Handy links:
*@Scottj1s is continuing to investigating remaining issues with unhandled exceptions and we’ll keep this issue open.
It’s worth pointing out that
System.AppDomain.CurrentDomain.UnhandledExceptiondoes fire for exceptions on background threads, even though exceptions on the main UI thread only fire onMicrosoft.UI.Xaml.Application.Current.UnhandledException.So the workaround is to hook both.
Closing this issue, having captured remaining issues
Here’s a little app that illustrates how ‘App.UnhandledException’ still only works partially in SDK1.2-preview2 :
https://github.com/k563/WinUI3_UnhandledExceptionTestApp
Maybe there’s still time for the WinUI team to address this show-stopper before the 1.2 release ?
Even when the exception is caught, it is loosing all important detail such as message and stack trace. See #7160
It really feels like WinUI is enormously underresourced. There are trivial bugs, and real show stopping bugs that have been kicking around for a loooong time. I’m hopeful this is going to change in the near future because I love the platform.
The latest WinUI3 + .NET9 + Desktop (<
WindowsPackageType>None</WindowsPackageType>) has the same situation - App.UnhandledException only catches exceptions from synchronous methods.[RelayCommand] public async Task FooAsync( ) {throw new NotImplementedException(); } - app crashes
[RelayCommand] public Task Foo( ) {throw new NotImplementedException(); } - App.UnhandledException catches
ICommand FooCommand { get; } FooCommand = new RelayCommand( () => _ = FooAsync()); - app does not crash, App.UnhandledException does not catch
🎉
Microsoft.WindowsAppSDK.1.2.221209.1has been released which fixes this issue.Handy links:
You might be interested in
TaskScheduler.UnobservedTaskException@Panchus