mfc
mfc

Reputation: 3026

iPhone C++/Objective-C exceptions

does anyone know the consequences of enabling C++ exception handling in XCode for iPhone apps? It appears to be disabled by default.

Are C++ and Objective C exceptions the same or can C++ exceptions only be caught by C++ code and vise versa?

thanks

Upvotes: 3

Views: 794

Answers (1)

justin
justin

Reputation: 104698

does anyone know the consequences of enabling C++ exception handling in XCode for iPhone apps?

It's the normal choice for C++ exception handling. If you are working with a C++ library, you will likely need this enabled.

You must not assume that exceptions will cleanly propagate across image boundaries. That is, you cannot assume that it is defined to throw through Foundation frames and catch on the other side. IOW, throw MyLib->through Foundation->catch in MyApp can result in undefined behavior. So, you must assume you need to catch these in MyLib.

Unlike OS X, the iPhone's C++ and ObjC exceptions are not implemented using 'Zero cost exceptions'. Zero cost exceptions, which favor execution speed over size, were not used because of the size of the unwind tables, and because of compiler support.

Enabling exceptions will require some speed and some size -- I expect that's less than 10% for most programs on iOS.

Ultimately, need should determine whether exceptions should be enabled or not, and they will introduce some execution and size overhead. Unless you are working with very specialized C++ libraries, you should assume that you need to enable them. However, the feature is supported.

Are C++ and Objective C exceptions the same or can C++ exceptions only be caught by C++ code and vise versa?

In 64 bit OS X and iOS environments, C++ and ObjC exceptions can be caught using either handler. In 32 bit OS X, they use different exception models. Therefore, the 'catch all' expression (written in either lang) can catch both C++ and ObjC exceptions in iOS and 64 bit OS X, but not in 32 bit OS X.

Upvotes: 2

Related Questions