8000 [NativeAOT] Add DefaultUncaughtExceptionHandler by jonpryor · Pull Request #9994 · dotnet/android · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,16 @@ 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);
}
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
0