Unhandled Exceptions
Avalonia does not offer any mechanism to handle exceptions globally and mark this as handled. The reason is, that one cannot know if the exception has been handled correctly and therefore the application may be in an invalid state. Instead it's highly recommend to handle exceptions locally if these can be handled by your application. That said it is still a good idea to log any unhandled exception for further support and debugging.
You can catch any exception from the main thread, which is also the UI thread in Avalonia, in your
Program.cs
-file. To do so we just wrap the entire void Main
in a try
and catch
block. In the catch
block you can log the error, inform the user, send the log file or restart the application.// File: Program.cs
public static void Main(string[] args)
{
try
{
// prepare and run your App here
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
catch (Exception e)
{
// here we can work with the exception, for example add it to our log file
Log.Fatal(e, "Something very bad happened");
}
finally
{
// This block is optional.
// Use the finally-block if you need to clean things up or similar
Log.CloseAndFlush();
}
}
If you are using
Task
s to run some work asynchronously, you can setup TaskScheduler.UnobservedTaskException
. For more information read the Microsoft .NET-Documentation.If you are using Avalonia together with ReactiveUI, you can subscribe to their
RxApp.DefaultExceptionHandler
. For more information please refer to ReactiveUI Default Exception Handler.Note,
RxApp.DefaultExceptionHandler
should be set before any ReactiveCommand was created. Otherwise, custom handler won't be used. You can set it in your application entry point or before any Avalonia view or window was created.Last modified 9mo ago