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?
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.
An object that represents a special condition that interrupts the normal flow of program execution.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With