Skip to content

[NativeAOT] Add DefaultUncaughtExceptionHandler #9994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions samples/NativeAOT/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ protected override void OnCreate(Bundle? savedInstanceState)
// An example of an Android API that uses a Java array
var list = new ColorStateList (new int[][] { [ 0, 1 ]}, [0, 1]);
Log.Debug ("NativeAOT", "MainActivity.OnCreate() ColorStateList: " + list);

var t = Intent?.Extras?.GetBoolean ("throw") ?? false;
if (t) {
throw new InvalidOperationException ("What happened?");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static void JNI_OnUnload (IntPtr vm, IntPtr reserved)
[UnmanagedCallersOnly (EntryPoint="Java_net_dot_jni_nativeaot_JavaInteropRuntime_init")]
static void init (IntPtr jnienv, IntPtr klass)
{
JniTransition transition = default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is a struct, and no need to check if it's "empty"? Seems like the methods are no-op:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, methods are a no-op if the struct "default constructor" is used. The non-default constructor can't be used until after a JniRuntime exists, which made structure "weird".

try {
var settings = new DiagnosticSettings ();
settings.AddDebugDotnetLog ();
Expand All @@ -50,9 +51,17 @@ static void init (IntPtr jnienv, IntPtr klass)

// Entry point into Mono.Android.dll
JNIEnvInit.InitializeJniRuntime (runtime);

transition = new JniTransition (jnienv);

var handler = Java.Lang.Thread.DefaultUncaughtExceptionHandler;
Java.Lang.Thread.DefaultUncaughtExceptionHandler = new UncaughtExceptionMarshaler (handler);
AndroidLog.Print (AndroidLogLevel.Info, "JavaInteropRuntime", $"init: Thread.DefaultUncaughtExceptionHandler={Java.Lang.Thread.DefaultUncaughtExceptionHandler}");
}
catch (Exception e) {
AndroidLog.Print (AndroidLogLevel.Error, "JavaInteropRuntime", $"JavaInteropRuntime.init: error: {e}");
transition.SetPendingException (e);
}
transition.Dispose ();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Java.Interop;

namespace Microsoft.Android.Runtime;

class UncaughtExceptionMarshaler (Java.Lang.Thread.IUncaughtExceptionHandler? OriginalHandler)
: Java.Lang.Object, Java.Lang.Thread.IUncaughtExceptionHandler
{
public void UncaughtException (Java.Lang.Thread thread, Java.Lang.Throwable exception)
{
var e = (JniEnvironment.Runtime.ValueManager.PeekValue (exception.PeerReference) as System.Exception)
?? exception;

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

// TODO: https://github.com/dotnet/runtime/issues/102730
// ExceptionHandling.RaiseUnhandledExceptionEvent(e);

OriginalHandler?.UncaughtException (thread, exception);
}
}
Loading