Hi. I discovered soe weird behavior. If the code return
in @finally block, it swallows exceptions.
Here’s full source code I tested with.
#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…”);
}
}
IMO, internal exception should be caught at external @catch, but actually it doesn’t.
Can I know the exact behavior of return
in @finally block?
Hi. I discovered soe weird behavior. If the code `return` in @finally block, it swallows exceptions.
This is actually fairly typical of finally blocks across languages. Something causes an exit from the protected block(s). It might be explicit control flow (break/continue/goto/return), implicit control flow (reaching the end of a protected block), or abnormal control flow (throwing an exception). Execution then begins at the beginning of the finally block. If control reaches the end of the finally block, the previous exit is resumed; otherwise, it is aborted and the new exit takes over.
Here's full source code I tested with.
#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...");
}
}
IMO, internal exception should be caught at external @catch, but actually it doesn't.
This would be a bit bizarre. A return statement in a finally block should return from the function, but only if the finally block wasn't entered during exception unwinding?
John.