Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between objc_exception_throw and [NSException raise]?

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?

like image 307
pepsi Avatar asked Jun 14 '12 17:06

pepsi


People also ask

What is the difference between Except and raise in Python?

raise allows you to throw an exception at any time. assert enables you to verify if a certain condition is met and throw an exception if it isn't. In the try clause, all statements are executed until an exception is encountered. except is used to catch and handle the exception(s) that are encountered in the try clause.

What is NSException in Swift?

An object that represents a special condition that interrupts the normal flow of program execution.


1 Answers

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.

like image 140
Tammo Freese Avatar answered Sep 21 '22 12:09

Tammo Freese