I'm using Xamarin.Mac to expose a large C#-based library to a native Objective-C application.
I do this by exposing wrappers objects of my managed classes decorated with [Register] and [Export] to the Objective-C runtime.
I know this is an unsupported scenario and there are other ways to embed the mono runtime in native applications. However, since the managed library I'm exposing to the Objective-C runtime is rather large and complex, I found it nearly impossible or just way to complicated to do this using this method: http://www.mono-project.com/docs/advanced/embedding/
Furthermore, the way I set it up using Xamarin.Mac works really well for me except for one thing: Exception handling.
The problem is that I can't figure out how to handle an exception thrown on the managed side in Objective-C. If I wrap my ObjC call that ends up being executed in managed code with @try @catch, the catch block is never hit and the application just crashes.
This is even the case when creating an NSException object in managed code and throwing it via [NSException raise] (Using PerformSelector or objc_msgSend since there's no binding built into Xamarin.Mac).
Another thing I tried was to construct the NSException object in managed code, pass it to a helper function in the native library and call [NSException raise] there. Same issue.
If however I raise an exception in ObjC without any managed code involved, I can handle it just fine using @try @catch so there seems to be some magic happening behind the scenes to marshal exceptions to the managed world when the call stack includes a managed function.
So, my question is: Is there any way I can handle an exception coming from the managed world in native ObjC code? While I could understand why it's not possible to handle a managed exception in native code, I can't accept that it's not possible to handle an NSException raised from either managed or native code just because there's a managed function involved in the call stack.