Objective-C try-catch - why is this compiling? And why is another debug vs release building coming back?

Found this interesting bit of code in the Objective-C category, which is used to capture NSExceptions and pass them as NSErrors to Swift code.

What I do not understand about this: 1) Why does it even compile? If an exception is thrown, there will never be a return value. 2) Why when compiling with debugging (no optimization level) and with release (minimum / fastest optimization level)?

- (BOOL)catchException:(void(^)())tryBlock error:(__autoreleasing NSError **)error {
   @try {
       tryBlock();
       return YES;
   }
   @catch (NSException *exception) {
       NSMutableDictionary *userInfo = [exception.userInfo mutableCopy];
       if (!userInfo) userInfo = [NSMutableDictionary new];
       userInfo[NSLocalizedDescriptionKey] = exception.reason;
       *error = [[NSError alloc] initWithDomain:exception.name code:0 userInfo:userInfo];
   }
   // Note the missing return value outside the try-catch
 }

Function call:

NSError *error;
BOOL result = [self catchException:^{
    @throw [NSException exceptionWithName:@"Exception" reason:@"WTF?" userInfo:nil];
} error:&error];

NSLog(@"Result: %@", result ? @"YES" : @"NO");

When compiling and starting using the debugging scheme, we get:

2017-02-09 10:01:39.695 Compile Test[23129:630118] Result: NO

And when you do the same with the release chart:

2017-02-09 10:01:39.695 Compile Test[23129:630118] Result: YES

, , try-catch, try-catch . ?!

+4
1

, " , , " ( ).

, undefined.

, - , , , ABI CPU, - .

; . , . ; "p myVariable" - , "myVariable" .

+3

Source: https://habr.com/ru/post/1669299/


All Articles