What is the life cycle of an object caught in the @catch block?

When you find an exception in an ObjC @catch block, what is the life cycle of this exception object? I know that I can safely use it inside a block, but what if I want to use it again after a block, like this?

 NSException * exception = nil; @try { // do something risky } @catch(NSException * e) { exception = e; } if (exception) { NSLog(@"Caught exception: %@", exception); } 

Is it safe to copy the link to another local one? Should I be retain, autorelease safe? Can I keep it and keep it on forever?

(It seems to work fine if I assign local or save and use later, but the documents do not discuss where this object β€œcomes” from the point of view of ownership or if it is special, so I was looking for more clarity.)

+4
source share
4 answers

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html

Almost all NSException objects (and other types of exception objects) are created as auto-implemented, which assigns them to the closest (to scale) auto-resource pool. When this pool is freed, the exception is destroyed.

In addition, I'm sure that somewhere in the memory programming guide , they mention that methods without new or alloc or copy in their names always return auto-implemented objects by convention. NSException methods are suitable for this.

A bit related (not NSException, but NSError):

If you create an NSError object using initWithDomain: code: userInfo :, you must send auto-advertisement to it before returning it to the caller.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html

+2
source
Blocks

@catch does nothing for the life cycle. The implicit contract here is the NSException object, which -raise or @throw n must be an auto-implemented object. This means that in the @catch block, the @catch that you are given is an auto-implemented object, just like any auto-implemented object that you can get from a method call. You can safely copy it in local and refer to it after the @catch block.

+1
source

NSException inherits from NSObject , so you can probably do all the typical things that could be done with any other Objective-C object.

However, I would recommend not doing anything with it outside of your flow. This O'Reilly exception article suggests:

Do not use the message release or autorelease to remove an NSException. All instances of NSException are placed in the main abstract. Manually deleting an instance will result in a SIGSEGV error.

Do not use the retain message to save an NSException. This will prevent the deletion of the auto resource pool from the instance instance. This will only lead to a thin memory leak.

... and some useful helpful hints for these objects.

0
source

NSException accepts NSCopying (and NSCoding fwiw). If you are in a dubious time of life and want to make it explicit, a copy will be perfect.

I stop there - unwinding and cocoa idioms flow against each other.

0
source

Source: https://habr.com/ru/post/1384431/


All Articles