Skip to content

Commit ec615ed

Browse files
authored
Enhance unhandled exceptions documentation
Updated documentation to include information about handling UI-thread exceptions with Dispatcher APIs.
1 parent dcc5383 commit ec615ed

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

docs/concepts/unhandledexceptions.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ description: CONCEPTS
44

55
# Unhandled Exceptions
66

7-
_Avalonia UI_ 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.
7+
Avalonia offer `Dispatcher.UIThread.UnhandledException` and `Dispatcher.UIThread.UnhandledExceptionFilter`. These APIs allow you to observe and optionally mark UI-thread exceptions as handled. However, this should be used with care: marking an exception as handled does not guarantee that the application can safely continue running. For this reason, it is still strongly recommended to catch and recover from exceptions locally when your application can reliably do so, and to use the global handlers primarily for logging, reporting, and last-resort mitigation.
8+
9+
Example:
10+
11+
```csharp
12+
Dispatcher.UIThread.UnhandledException += (s, e) =>
13+
{
14+
Console.WriteLine($"Unhandled: {e.Exception}");
15+
// e.Handled = true; // Optional, use carefully
16+
};
17+
```
818

919
## Logging
1020

0 commit comments

Comments
 (0)