I'm writing handler for Unhandled Exception event which we will dump an exception to a file and then send to a server for further analyze. For now it will just show that crash had happen. I subscribed to the event. In that method I called a function for handling errors. However, when I wrote in the method this code:
public async Task HandleException(Exception exception)
{
var dialog = new MessageDialog(exception.Message, "Exception occurred");
await dialog.ShowAsync();
}
and run it in debug mode the Visual Studio shows Visual Studio Just-In-Time Debugger. In the first place I thought it was the problem that I'm trying to show Message Box when the GUI thread is dying. I changed the function to:
public async Task HandleManagedException(Exception
{
await FileStorage.WriteToFileAsync("someFile.txt", exception.ToString());
}
where function FileStorage.WriteToFileAsync looks like:
public async Task WriteToFileAsync(string filename, string data)
{
var file = await this.storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, data);
}
In debugger mode, when I put breakpoint on await FileIO.WriteTextAsync(file,data) it stops there. After pressing continue button, the same window shows like in the previous code. It is running properly when it is called from XAML events. I've searched the google and StackOverflow for the async methods in unhandled exception handler, but there was nothing related to my problem.
My questions are: What it is the reason for that error? Is it possible to use async methods in the Unhandled exception handler?
Update: Thank you for answers so far. The second question I answered myself after close debugging the code. It turned out that it is possible and the function is working as intended.
To avoid the Visual Studio Just-In-Time Debugger dialog, we can set UnhandledExceptionEventArgs.Handled property to true like following:
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
this.UnhandledException += App_UnhandledException;
}
private async void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
e.Handled = true;
await HandleException(e.Exception);
}
public async Task HandleException(Exception exception)
{
var dialog = new MessageDialog(exception.Message, "Exception occurred");
await dialog.ShowAsync();
}
In debug mode, you get the Warning and Just-In-Time Debugger dialog because there are following codes in App.g.i.cs:
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached)
global::System.Diagnostics.Debugger.Break();
};
#endif
These codes are generated automatically when we build the project. And from these codes, we can find while in debug model, if there is any unhandled exception, it will call Debugger.Break Method and this will result in the Warning and Just-In-Time Debugger dialog.
Setting e.Handled = true; in our handler can avoid this. For more info, you can check this similar question: How to try/catch all exceptions. However, as you can see this will only happen in debug mode, in release model, your code should work well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With