12

I am thinking about pros and cons of Try-Catch in Objective-C. According to this article Dispelling NSException Myths in iOS: Can We Use @try…@catch, @finally?, try-catch isn't that bad, except for it leaks memory in ARC.

So how does try-catch cause memory leak?

3
  • 2
    because ObjC is not exception-safe by default (there is a flag to make it exception-safe but can't remember details)
    – Bryan Chen
    Commented Nov 26, 2014 at 3:38
  • 4
    The article, if it says that, is wrong. Every time someone at Apple speaks about this, they say very clearly: "Don't throw exceptions as a way of doing flow control; exceptions are for exceptional, unwanted situations and mean you want to crash." If you think you need to throw your own exception, just think again. (Having said that, I wish the people at the AVFoundation development office would get the memo.)
    – matt
    Commented Nov 26, 2014 at 3:42
  • Saved my life just knowing that it does!
    – mllm
    Commented Nov 1, 2016 at 12:51

1 Answer 1

20

First of all: Exceptions have different semantics in Objective-C. An exception means that something went completely wrong because of a programming mistake and the further execution of the application is not useful. Terminate it! To handle "expected errors" (like insufficient user input or not responding servers et al.) use Cocoa's error handling pattern. (The reason for this is that exceptions seems to be convenient in many situation, but are very hard to handle in other situations, i. e. while object construction. Read about exceptions in C++. It is painful.)

To your Q: ARC adds additional code to handle memory management. This code has to be executed to handle the memory management, esp. to release objects. If an exception occurs before this is done, the control flow never reaches the release statement. Memory leaks.

- (void)method
{
   id reference = …;
   // Some ARC code to retain the object, reference points to.
   … 
   @throw …
   …
   // reference loses its extent, because of method termination
   // Some ARC code to release the object, reference points to.
}

If you have an exception, the method is left immediately and the ARC code and the end of the method to release the object is never executed. This is the leak.

You can change this behavior by compiling the source with -fobjc-arc-exceptions option.

http://clang.llvm.org/docs/AutomaticReferenceCounting.html#exceptions

This will add code to make ARC exception-safe causing a runtime penalty. But there is little reason to do so in Cocoa development, as explained at the beginning of this answer.

4
  • 1
    Exceptions do not have different semantics in Obj-C than in C++. In fact they're implemented identically, the behavior is defined by the platform ABI. What you mean to say is that exceptions have different semantics in Cocoa, or rather, the convention in [almost all of] Cocoa is to reserve exceptions only for fatal errors.
    – Jens Alfke
    Commented May 24, 2019 at 19:45
  • Obviously "implementation" and "semantics" are two different things. Therefore, the statement "Exceptions do not have different semantics in Obj-C than in C++. In fact they're implemented identically," makes no sense. I described the difference in the A.There is no reason to repeat the explanation in a comment. Commented May 25, 2019 at 6:22
  • 1
    I don't agree that exception should always be fatal in objective c. An example is NSSecureCoding which will throw an exception if some data cannot be decoded. The data can be outside of your control and an application should certainly not crash on data that it cannot handle. Commented Jun 20, 2019 at 10:50
  • Exceptions are resource-intensive in Objective-C. You should not use exceptions for general flow-control, or simply to signify errors. from developer.apple.com/library/archive/documentation/Cocoa/… But, yes, sometimes even Apple does not follow its own rules. Thean you have to deal with it. Commented Jun 24, 2019 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.