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?

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
allenlinli
  • 2,066
  • 3
  • 27
  • 49
  • 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 Nov 26 '14 at 03: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 Nov 26 '14 at 03:42
  • Saved my life just knowing that it does! – mllm Nov 01 '16 at 12:51

1 Answers1

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.

Joshua Gross
  • 526
  • 6
  • 10
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • 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 May 24 '19 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. – Amin Negm-Awad May 25 '19 at 06: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. – Werner Altewischer Jun 20 '19 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 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocExceptionHandling.html#//apple_ref/doc/uid/TP30001163-CH13 But, yes, sometimes even Apple does not follow its own rules. Thean you have to deal with it. – Amin Negm-Awad Jun 24 '19 at 14:24