Why is @throw an expensive operation in Objective-C?

I heard that @throw is expensive in Objective-C, what is the reason for this? Is throw

Java expensive too?

+3


source to share


2 answers


"Exceptions in Objective C are implemented using the C primitive: longjmp (). Objective-C is not C ++. You can have many levels of method invocation between the code that throws the exception and the method that catches it. It's very easy to write a leak memory. "

http://newsgroups.derkeiler.com/Archive/Comp/comp.sys.mac.programmer.help/2007-08/msg00020.html

Also...

"A little more information.

C ++ exceptions and, according to modern ABIs, Objective-C exceptions are extremely cheap to set up (@try), but @throw and @catch are expensive.



When @throw happens, there is a high cost of generating bits to properly unwind the stack.

Unfortunately, AppKit has an issue where it invokes conversation information to be generated as a normal part of its job (no exception).

Thus, some 64bit AppKit operations can be quite slow at this time.

b.bum "

http://www.cocoabuilder.com/archive/cocoa/217947-cocoa-application-running-very-slow-under-64-bit.html

+3


source


Modern Objective-C uses C ++ exceptions. While there is a cost associated with throwing a C ++ exception , it is hardly prohibitive in most cases.

Cause exceptions are not used with Objective-C due to memory management.



Languages ​​such as Java and C ++ have built-in mechanisms for managing memory when an exception is thrown. Objective-C is not (unless you wrap all C ++ stack objects).

Instead, when you're throw

in Objective-C, all strong references inside the closing block try

just leak out. There are ways to code around this, but it is very complex and error prone and therefore unreasonable in practice.

+3


source







All Articles