O'Reilly logo

Advanced Mac OS X Programming: The Big Nerd Ranch Guide by Mark Dalrymple

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exception Handling in Cocoa

Cocoa provides two exception-handling mechanisms similar to what C++ and Java offer. The older, classic way to handle exceptions is to wrap a body of code with a set of macros. The newer way, the native way, was introduced in Mac OS X 10.3 and adds language support for exceptions.

Classic exception handling

To handle exceptions, wrap your code in an NS_DURING clause. Any exceptional conditions will raise an exception, which can then be caught by an NS_HANDLER clause.

The way you work it is:

NS_DURING     ... code that might throw an exception NS_HANDLER     ... code to examine the exception and possibly handle it NS_ENDHANDLER

If any code between NS_DURING and NS_HANDLER raises an exception, execution immediately ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required