Skip to content

Instantly share code, notes, and snippets.

@nonsensery
Created January 9, 2017 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nonsensery/89668e216adeed187cabe974069614d2 to your computer and use it in GitHub Desktop.
Save nonsensery/89668e216adeed187cabe974069614d2 to your computer and use it in GitHub Desktop.
Swift-Objc try/catch bridge
objc_try_catch({
let exception = NSException(name: "Something went wrong", reason: "not sure", userInfo: [:])
print("raising NSException: \(exception)...")
exception.raise()
}) { exception in
print("... caught NSException: \(exception)")
}
extern void objc_try_catch(__attribute__((noescape)) void(^tryBlock)(), __attribute__((noescape)) void(^catchBlock)(NSException *));
void objc_try_catch(__attribute__((noescape)) void(^tryBlock)(), __attribute__((noescape)) void(^catchBlock)(NSException *)) {
@try {
tryBlock();
}
@catch (NSException *exception) {
catchBlock(exception);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment