microsoft / microsoft/microsoft-ui-xaml
Application.UnhandledException does not catch async void exceptions if the app contains an Expander control in its tree
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### Describe the bug
The global exception handler should catch async void method exceptions, but there is a specific case in which it still doesn't, making the app crash. Async void problems were fixed at some point ago according to the issue https://github.com/microsoft/microsoft-ui-xaml/issues/10447, but it seems not completely.
### Why is this important?
It's important because it ruins the user experience. I have handled a few of my known exceptions gracefully in the global exception handler and because of this issue, even if handled, they are not catched and the app crashes instead of informing the user of what's wrong. Besides this, the error is not even logged, all you get being a generic `Exception code: 0xc000027b` in the Event Viewer, making the developer unaware of issues they might have.
### Steps to reproduce the bug
Just create a sample app having the latest Windows App SDK 2.3.1, with the following additions:
_MainWindow.xaml_
```XML
Click me to crash
I'm here just to make the app crash
```
_MainWindow.xaml.cs_
```C#
private async void Button_Click(object sender, RoutedEventArgs e)
{
throw new Exception("You won't catch me");
}
```
_App.xaml.cs_
```C#
public App()
{
InitializeComponent();
UnhandledException += App_UnhandledException;
}
...
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
Debug.WriteLine(e.Message);
}
```
### Actual behavior
The app is crashing on a very simple exception being thrown, because of the use of an `Expander` control.
### Expected behavior
The app should catch the thrown exception, even if an `Expander` is used.
### Screenshots
_No response_
### NuGet package version
2.3.1
### Windows version
Windows 11 (25H2): Build 26200
### Additional context
I'm not sure why the `Expander` control is causing this behavior - this is beyond my area of expertise.
Contributor guide
Research direction
Start by reproducing the issue with MainWindow.xaml, MainWindow.xaml.cs, and App.xaml.cs using Windows App SDK 2.3.1. Read the Application.UnhandledException handler and the Button_Click path, then compare behavior with and without the Expander. Done means the async void exception is caught and reported by the handler while the Expander remains in the tree.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100