Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a @try-@catch-@finally block, is it good use finally or to continue normally?

This is a simple Objective-C question.

When you use a @trythe work flow can run in 2 ways

  • If some NSException appear, the code immediately jump to @catch block and than to @finally
  • If not, finish to run the @try block and than run @finally

So, what is the difference to use or not use the @finally block? If I use only:

-(void)function{
    @try {
     ...
    }
    @catch (NSException *exception) {
     ...
    }
    >>>The workflow will run this line in any case?
}

Than the rest of the function will run, or only the @catch block if a NSException is created?

like image 756
Rodrigo Avatar asked Jan 11 '12 13:01

Rodrigo


3 Answers

"A @finally block contains code that must be executed whether an exception is thrown or not." Does code in finally get run after a return in Objective-C?

The finally block exists to release/clean up resources like open sockets, open files, database locks, semaphore locks, and so on.

If an error occurs inside of the catch block or the catch block rethrows the exception, then the line:

>>>The workflow will run this line in any case?

is not executed. However, the code in the finally block should be executed. The finally block is the last, best chance to exit cleanly from an application that is about to crash. Even if the application is not about to crash, it is still the best place to cleanup resources because the code inside the finally block is more likely to be executed under unexpected conditions than code outside of the finally block.

like image 53
ahoffer Avatar answered Nov 01 '22 01:11

ahoffer


A couple things to note:

  1. The @catch block is not required, you can have @try-@finally, and use the @finally block for anything (e.g. cleanup) that must happen even if an exception occurs
  2. The @catch block doesn't have to catch NSException, it may (and probably should) be changed to catch more specific exceptions. In that case the @catch block, along with the code below the @try-@catch-@finally, would not be run depending on the exception
like image 34
Nathanial Woolls Avatar answered Nov 01 '22 02:11

Nathanial Woolls


Few important points that were missed in other's answers here.

  • Apple does NOT recommend the use of @try @catch @finally clauses in production code. Their penalty is too high, and the real benefits are few. In C++ where defensive code is all over the place, you can design your application to be "exception based" meaning, all the code is designed for stack-rollback and throwing and rethrowing exceptions until they reach the top level of the stack. This is NOT the case in Obj-C, and even in C++ this paradigm kills most compiler optimizations, because the compiler cannot shortcut any scenario as an exception can break it in the middle.
  • However --- Apple provides the try/catch/finally mechanism in Obj-C, so you can create debug-configuration-only code, that will help you identify and catch (literally) your bugs BEFORE releasing the application to the public.
  • In addition Apple provides a complete (and beautiful) "Error handling" paradigm and protocol, backed up in API (NSError object, nested NSErrors NSError recovery protocol, NSError display API etc.) that is suitable for runtime error handling in your application - in "release" builds of your applications.
  • The above is correct for both iOS and MacOS-X error handling.

So, the whole discussion about the use of @finally here are a little exaggerated.

like image 27
Motti Shneor Avatar answered Nov 01 '22 03:11

Motti Shneor