Skip to content

Commit a8a573c

Browse files
authored
[NativeAOT] Raise AppDomain.UnhandledException event (#10092)
Context: 9ad492a Context: dotnet/runtime#102730 Commit 9ad492a had a TODO: > TODO: once dotnet/runtime#102730 is fixed, update > `UncaughtExceptionMarshaler` to do whatever it needs to do to cause > the `AppDomain.UnhandledException` event to be raised. dotnet/runtime#102730 *has* been fixed. Update `UncaughtExceptionMarshaler` to call `ExceptionHandling.RaiseAppDomainUnhandledExceptionEvent(object)` with the unhandled exception, so that the `AppDomain.UnhandledException` event is raised. Update `samples/NativeAOT` to subscribe to the `AppDomain.UnhandledException` event. Install the app: ./dotnet-local.sh build -t:Install samples/NativeAOT/NativeAOT.csproj and run the sample so that it throws an unhandled exception: adb shell am start --ez throw 1 net.dot.hellonativeaot/my.MainActivity and now `adb logcat` contains: I NativeAotFromAndroid: AppDomain.UnhandledException! I NativeAotFromAndroid: sender: [False ] I NativeAotFromAndroid: e.IsTerminating: True I NativeAotFromAndroid: e.ExceptionObject: System.InvalidOperationException: What happened? I NativeAotFromAndroid: at NativeAOT.MainActivity.OnCreate(Bundle savedInstanceState) + 0x2f4 I NativeAotFromAndroid: at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) + 0xc8 showing that the `AppDomain.UnhandledException` event was raised.
1 parent 395189c commit a8a573c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

samples/NativeAOT/MainApplication.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,12 @@ public override void OnCreate ()
1919
Log.Debug ("NativeAOT", "Application.OnCreate()");
2020

2121
base.OnCreate ();
22+
23+
AppDomain.CurrentDomain.UnhandledException += (sender, e) => {
24+
Console.WriteLine ("AppDomain.UnhandledException!");
25+
Console.WriteLine ($" sender: {sender} [{sender != null} {sender?.GetType ()}]");
26+
Console.WriteLine ($" e.IsTerminating: {e.IsTerminating}");
27+
Console.WriteLine ($" e.ExceptionObject: {e.ExceptionObject}");
28+
};
2229
}
2330
}

src/Microsoft.Android.Runtime.NativeAOT/UncaughtExceptionMarshaler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.ExceptionServices;
2+
13
using Java.Interop;
24

35
namespace Microsoft.Android.Runtime;
@@ -12,8 +14,7 @@ public void UncaughtException (Java.Lang.Thread thread, Java.Lang.Throwable exce
1214

1315
AndroidLog.Print (AndroidLogLevel.Fatal, "DOTNET", $"FATAL UNHANDLED EXCEPTION: {e}");
1416

15-
// TODO: https://github.com/dotnet/runtime/issues/102730
16-
// ExceptionHandling.RaiseUnhandledExceptionEvent(e);
17+
ExceptionHandling.RaiseAppDomainUnhandledExceptionEvent(e);
1718

1819
OriginalHandler?.UncaughtException (thread, exception);
1920
}

0 commit comments

Comments
 (0)