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?

Community
  • 1
  • 1
pepsi
  • 6,785
  • 6
  • 42
  • 74

2 Answers2

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.

Tammo Freese
  • 10,514
  • 2
  • 35
  • 44
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

TypingPanda
  • 1,607
  • 3
  • 19
  • 32