[cfe-dev] returning in @finally block swallows exception throwing.

John McCall rjmccall at apple.com
Sat Aug 11 16:55:55 PDT 2012


On Aug 11, 2012, at 12:13 PM, Hoon Hwangbo wrote:
> 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.



More information about the cfe-dev mailing list