NMBExceptionCapture.m 851 B

1234567891011121314151617181920212223242526272829303132333435
  1. #import "NMBExceptionCapture.h"
  2. @interface NMBExceptionCapture ()
  3. @property (nonatomic, copy) void(^ _Nullable handler)(NSException * _Nullable);
  4. @property (nonatomic, copy) void(^ _Nullable finally)(void);
  5. @end
  6. @implementation NMBExceptionCapture
  7. - (nonnull instancetype)initWithHandler:(void(^ _Nullable)(NSException * _Nonnull))handler finally:(void(^ _Nullable)(void))finally {
  8. self = [super init];
  9. if (self) {
  10. self.handler = handler;
  11. self.finally = finally;
  12. }
  13. return self;
  14. }
  15. - (void)tryBlock:(__attribute__((noescape)) void(^ _Nonnull)(void))unsafeBlock {
  16. @try {
  17. unsafeBlock();
  18. }
  19. @catch (NSException *exception) {
  20. if (self.handler) {
  21. self.handler(exception);
  22. }
  23. }
  24. @finally {
  25. if (self.finally) {
  26. self.finally();
  27. }
  28. }
  29. }
  30. @end