Skip to content

Commit 305b52d

Browse files
committed
Added handle for UnhandledExceptions
1 parent 9837d36 commit 305b52d

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

HttpGamepadInput/App.xaml.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
1-
namespace HttpGamepadInput;
1+
using System.Diagnostics;
2+
3+
namespace HttpGamepadInput;
24

35
public partial class App : Application
46
{
57
public App()
68
{
79
InitializeComponent();
10+
11+
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
12+
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
13+
814
}
915

1016
protected override Window CreateWindow(IActivationState? activationState)
1117
{
1218
return new Window(new AppShell());
1319
}
20+
21+
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
22+
{
23+
Exception? ex = e.ExceptionObject as Exception;
24+
Debug.WriteLine($"[UNHANDLED] {ex?.Message}");
25+
MainThread.BeginInvokeOnMainThread(async () =>
26+
{
27+
await Application.Current?.MainPage?.DisplayAlert("Error", $"[UNHANDLED] {ex?.Message}", "OK");
28+
});
29+
}
30+
31+
private void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
32+
{
33+
Debug.WriteLine($"[TASK ERROR] {e.Exception.Message}");
34+
e.SetObserved();
35+
MainThread.BeginInvokeOnMainThread(async () =>
36+
{
37+
await Application.Current?.MainPage?.DisplayAlert("Task error", $"[TASK ERROR] {e.Exception.Message}", "OK");
38+
});
39+
}
1440
}

HttpGamepadInput/AppShell.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<FlyoutItem Title="Gamepads">
1111
<ShellContent ContentTemplate="{DataTemplate views:MainPage}"
12-
Title="1 Gamepad"
12+
Title="HTTP Gamepad"
1313
Route="MainPage" />
1414
</FlyoutItem>
1515
<FlyoutItem Title="Settings">

0 commit comments

Comments
 (0)