I discovered weird behavior of Xcode.
Xcode debugger doesn’t break for uncaught exception in this code.
@try { @throw @"AA"; }
@catch (...) { @throw; }
@finally { return; }
But exception in this code caught and trigger Xcode break execution for debugging.
@try { @throw @"AA"; }
@catch (...) { @throw; }
@finally { }
If @finally block returns debugger can’t catch the exception. Have you ever seen this problem? I’m not sure this is really an issue. By the perspective it looks like designed behavior. I don’t know why. Shouldn’t I return in @finally block? My problem is it swallows exception silently, so I can’t detect it.
Shame on me, I don’t know well try…catch…finally behaviors. I almost haven’t used exception catching code. Is this designed behavior or buggy behavior? Is this any known issue?
Here’s my environment.
- Xcode Version 4.4 (4F250)
- OS X 10.7.4
Edit
I attach full test source code.
#import <Foundation/Foundation.h>
int main (int a, char** b)
{
@try
{
NSLog(@"trying something...");
@try { @throw @"AA"; }
@catch (...) { @throw; }
@finally { return 0; }
}
@catch (...)
{
NSLog(@"something catched.");
}
@finally
{
NSLog(@"finally...");
}
}
Putting a
returnin a @finally block seems like a bad idea. The exception handling mechanism is going to try to unwind the call stack as it deals with the exception you’re throwing. If a @finally block changes what’s on the stack, you undermine the exception handler. It doesn’t seem at all surprising that this crashes.Also, as bbum pointed out, exceptions aren’t used for flow control in Cocoa and Cocoa Touch. Throwing an exception through a Cocoa method usually fails. Even if what you’re doing is supposed to work in generic Objective-C, it would probably still cause problems in real code.
Short answer: Don’t do that.