Skip to content

Instantly share code, notes, and snippets.

@janodev
Last active April 16, 2018 09:18
Show Gist options
  • Save janodev/f508ca499a8f276d60064db8631bd9ce to your computer and use it in GitHub Desktop.
Save janodev/f508ca499a8f276d60064db8631bd9ce to your computer and use it in GitHub Desktop.
Capture ObjC exceptions. From https://stackoverflow.com/a/36454808/412916
#import <Foundation/Foundation.h>
@interface ObjC: NSObject
+ (BOOL)catchException:(void(^)(void))tryBlock error:(__autoreleasing NSError **)error;
@end
#import "ObjC.h"
@implementation ObjC
+ (BOOL)catchException:(void(^)(void))tryBlock error:(__autoreleasing NSError **)error {
@try {
tryBlock();
return YES;
}
@catch (NSException *exception) {
*error = [[NSError alloc] initWithDomain:exception.name code:0 userInfo:exception.userInfo];
return NO;
}
}
@end
/// Return a storyboard for the given name and bundle. Log a console error if not found.
private static func createStoryboard(name: String, bundle: Bundle) -> UIStoryboard? {
var storyboard: UIStoryboard? = nil
do {
try ObjC.catchException {
storyboard = UIStoryboard(name: name, bundle: bundle)
}
} catch {
print("🚨 Failed to create storyboard \(name): \(error)")
}
return storyboard
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment