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 ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.