7

One useful tip I've been using for XCode is adding breakpoints on exceptions.

I was wondering why we need to add two breakpoints--one for objc_exception_throw and one for [NSException raise].

What cases do one cover that the other doesn't?

2 Answers 2

4

You should only use a breakpoint on objc_exception_throw. The method -[NSException raise] calls objc_exception_throw, so objc_exception_throw covers all cases that -[NSException raise] covers. The other way around is not true: The @throw directive is compiled to call objc_exception_throw directly. This method shows the difference:

- (void)throwAndCatch
{
    @try {
        NSException *exception = [[NSException alloc] initWithName:@"Test" 
                                                            reason:@"test" 
                                                          userInfo:nil];
        @throw exception;
    }
    @catch (NSException *exception) {
        NSLog(@"Caught");
    }
}

When calling -throwAndCatch, a breakpoint on -[NSException raise] has no effect, while a breakpoint on objc_exception_throw will work.

0

Here is what from Apple's Documentation about Exception:

An important difference between @throw and raise is that the latter can be sent only to an NSException object whereas @throw can take other types of objects as its argument (such as string objects). Cocoa applications should @throw only NSException objects.

Which means if you are implementing a Cocoa applciation project and if you strictly follow that @throw only NSExeption objects they are the same.

reference: Link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.