Skip to content

Commit 4acb844

Browse files
authored
[runtime] Adjust exception handling to always return managed exceptions if so requested. (#15432)
In the following scenario: * Objective-C exception mode is to throw a managed exception. * The xamarin_process_nsexception_using_mode was given a pointer to store any resulting exceptions. * The Objective-C exception didn't already have an associated managed exception. We'd throw the managed exception upon return to managed code instead of returning the managed exception. With this change the xamarin_process_nsexception_using_mode will always return the resulting managed exception in the scenario above (this way the behavior is identical independent of whether the Objective-C exception already has an associated managed exception or not).
1 parent 76fbc89 commit 4acb844

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

runtime/runtime.m

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,32 +2240,33 @@ -(void) xamarinSetFlags: (enum XamarinGCHandleFlags) flags;
22402240
break;
22412241
case MarshalObjectiveCExceptionModeThrowManagedException:
22422242
exc_handle = [[ns_exception userInfo] objectForKey: @"XamarinManagedExceptionHandle"];
2243+
GCHandle handle;
22432244
if (exc_handle != NULL) {
2244-
GCHandle handle = [exc_handle getHandle];
2245+
GCHandle e_handle = [exc_handle getHandle];
22452246
MONO_ENTER_GC_UNSAFE;
2246-
MonoObject *exc = xamarin_gchandle_get_target (handle);
2247-
mono_runtime_set_pending_exception ((MonoException *) exc, false);
2247+
MonoObject *exc = xamarin_gchandle_get_target (e_handle);
2248+
handle = xamarin_gchandle_new (exc, false);
22482249
xamarin_mono_object_release (&exc);
22492250
MONO_EXIT_GC_UNSAFE;
22502251
} else {
2251-
GCHandle handle = xamarin_create_ns_exception (ns_exception, &exception_gchandle);
2252+
handle = xamarin_create_ns_exception (ns_exception, &exception_gchandle);
22522253
if (exception_gchandle != INVALID_GCHANDLE) {
22532254
PRINT (PRODUCT ": Got an exception while creating a managed NSException wrapper (will throw this exception instead):");
22542255
PRINT ("%@", xamarin_print_all_exceptions (exception_gchandle));
22552256
handle = exception_gchandle;
22562257
exception_gchandle = INVALID_GCHANDLE;
22572258
}
2259+
}
22582260

2259-
if (output_exception == NULL) {
2260-
MONO_ENTER_GC_UNSAFE;
2261-
MonoObject *exc = xamarin_gchandle_get_target (handle);
2262-
mono_runtime_set_pending_exception ((MonoException *) exc, false);
2263-
xamarin_mono_object_release (&exc);
2264-
xamarin_gchandle_free (handle);
2265-
MONO_EXIT_GC_UNSAFE;
2266-
} else {
2267-
*output_exception = handle;
2268-
}
2261+
if (output_exception == NULL) {
2262+
MONO_ENTER_GC_UNSAFE;
2263+
MonoObject *exc = xamarin_gchandle_get_target (handle);
2264+
mono_runtime_set_pending_exception ((MonoException *) exc, false);
2265+
xamarin_mono_object_release (&exc);
2266+
xamarin_gchandle_free (handle);
2267+
MONO_EXIT_GC_UNSAFE;
2268+
} else {
2269+
*output_exception = handle;
22692270
}
22702271
break;
22712272
case MarshalObjectiveCExceptionModeAbort:

0 commit comments

Comments
 (0)