pub unsafe fn catch<R>(
    closure: impl FnOnce() -> R + UnwindSafe
) -> Result<R, Option<Id<Exception, Shared>>>
Available on crate feature exception only.
Expand description

Tries to execute the given closure and catches an Objective-C exception if one is thrown.

This is the Objective-C equivalent of Rust’s catch_unwind. Accordingly, if your Rust code is compiled with panic=abort this cannot catch the exception.

Returns a Result that is either Ok if the closure succeeded without an exception being thrown, or an Err with the exception. The exception is automatically released.

The exception is None in the extremely exceptional case that the exception object is nil. This should basically never happen, but is technically possible on some systems with @throw nil.

Safety

The given closure must not panic (e.g. normal Rust unwinding into this causes undefined behaviour).

Additionally, this unwinds through the closure from Objective-C, which is undefined behaviour until C-unwind is stabilized, see RFC-2945 - you can try this out on nightly using the unstable-c-unwind feature flag.