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 Mainin a
catchblock. In the
catchblock 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)
// prepare and run your App here
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");
// This block is optional.
// Use the finally-block if you need to clean things up or similar
If you are using
Tasks 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.
RxApp.DefaultExceptionHandlershould 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.