This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2019-07-25 19:30:14 +08:00

36 lines
851 B
Objective-C

#import "NMBExceptionCapture.h"
@interface NMBExceptionCapture ()
@property (nonatomic, copy) void(^ _Nullable handler)(NSException * _Nullable);
@property (nonatomic, copy) void(^ _Nullable finally)(void);
@end
@implementation NMBExceptionCapture
- (nonnull instancetype)initWithHandler:(void(^ _Nullable)(NSException * _Nonnull))handler finally:(void(^ _Nullable)(void))finally {
self = [super init];
if (self) {
self.handler = handler;
self.finally = finally;
}
return self;
}
- (void)tryBlock:(__attribute__((noescape)) void(^ _Nonnull)(void))unsafeBlock {
@try {
unsafeBlock();
}
@catch (NSException *exception) {
if (self.handler) {
self.handler(exception);
}
}
@finally {
if (self.finally) {
self.finally();
}
}
}
@end