I have some Xamarin.Mac code that calls a native function as described here. When the function is called it sometimes causes an NSException to be thrown (dependent on connected hardware). Unfortunately the exception isn't caught using try/catch and it crashes my app.
For now I can live with the circumstances that give rise to the exception. However, I cannot live with my app crashing.
I wish to be able to trap when an NSException is thrown, perhaps report an error and continue.
Something like the following would do it:
ExceptionCatcher.CatchExceptionAndGetError(
() => _platformThing.SometimesThrowException(),
out var error);
if (error != null)
{
// Report error
}
// Carry on
I have verified that the exception can be caught when using Objective-C directly.
I am aware of the NSException wrapping solution here. However, before attempting to implement something like this to work with my code, is there something that already exists for Xamarin that I can use to handle this situation?