In what circumstances is @finally non-redundant in Cocoa's try/catch/finally Exception Handling?

In that case, where you’re squashing the exception, none finally is used to clean up when you either don’t catch the exception, or rethrow it, in either case leaving final exception response to calling code. Since exceptions in Cocoa are only supposed to be used for programming errors and thus occur rarely, this is an entirely reasonably thing to do.

In that case, where you’re squashing the exception, none. @finally is used to clean up when you either don’t catch the exception, or rethrow it, in either case leaving final exception response to calling code. Since exceptions in Cocoa are only supposed to be used for programming errors and thus occur rarely, this is an entirely reasonably thing to do.

It’s also worth pointing out one case where you don’t need to use @finally, which is when you set up your own autorelease pool. When the “parent” autorelease pool is destroyed, any inner ones that haven’t been cleaned up yet will also be. If you do try to clean it up yourself, you need to promote the exception itself out of your autorelease pool.

FYI, here is Apple's doc detailing how to perform memory management with @try/@catch/@throw/@finally exception handling: developer.apple. Com/mac/library/documentation/Cocoa/Conceptual/… – Dave Gallagher Jul 1 '10 at 0:07.

In those scenarios there is no difference because the exception is swallowed. Here are two scenarios where there is a difference: obj cleanUp is called: MyClass *obj; @try { obj doSomething; } @catch (NSException * e) { @throw; } @finally { obj cleanUp; // called when exception is caught } obj cleanUp is not called: MyClass *obj; @try { obj doSomething; } @catch (NSException * e) { @throw; } obj cleanUp; // not called when exception is caught.

When: You are not catching the type of exception that occured You caught the exception, but the code in the catch block also throws an exception.

It's also worth noting that code in @finally blocks will run when control exits the @try block for any reason, even via return or goto. For example: @try { doStuff(); if(bail){ return; } doMoreStuff(); } @finally { obj cleanUp; } obj announceSuccess; obj cleanUp will execute even if bail is true, but obj announceSuccess will not.

The try/catch/finally approach is widely used in Java but hardly ever used in Objective-C, and is not a preferred approach - you simply do not need it as libraries will not throw exceptions the way Java library calls would, and if you are writing your own libraries you should not expect callers will naturally think to look for exceptions to catch. The convention most widely used and understood is that of a delegate that has an error method callback, or perhaps notifications for more general failures that you need to pass up through multiple levels of code. That approach might be more widely used in the Java world if there were a simple notification system the way Cocoa has it set up.

The delegate approach has the same documenting property as declaring an exception in Java does, they are just different approaches but it's generally better to use an approach more suited to the language at hand unless there's a very compelling reason to do otherwise.

I am currently working with try/catch/finally since Apple's Scripting Bridge in OS X 10.5 regularly fails by throwing exceptions. If you use Scripting Bridge, you can't avoid exceptions. I fully agree though, for most Cocoa/Objective-C code, exceptions are not the best way of handling errors.

– Nick Forge Apr 14 '09 at 5:03.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions