Brief Intro to Exception Handling of Foundation Framework

来源:互联网 发布:mysql insert 函数 编辑:程序博客网 时间:2024/04/28 07:15

Exception Processing

Objective-C provides mechanisms for handling exception conditions during program execution. An exception condition can be defined as an unrecoverable programming or runtime error. Examples include errors such as an unimplemented (abstract) method, or runtime errors such as an out-of-bounds collection access. The compiler directives @try@catch()@throw, and @finally provide runtime support for exception handling, and the NSException class encapsulates informationpertinent to an exception.

When an exception is raised, program control flow transfers to the local exception handler. The program stack frame is also unwound between where the exception was raised and where it was caught and handled. As a result, resources that are not automatically managed (e.g., Foundation Framework objects, objects created using manual reference counting, and any C language–specific resources, such as structures) may not be cleaned up properly. Specifically, the Foundation Framework APIs are not exception-safe; thus if used within an exception-handling domain and an exception is thrown, they may leak memory and/or have corrupted content. Thus, the general rule is that when an exception is raised, no attempt at recovery should be made and the application should be exited promptly.

The @try@catch, and @finally directives make up a control structure for code that executes within the boundaries of the exception handling logic. The @try directive defines a statement block (also referred to as an exception-handling domain) that can potentially throw an exceptionThe @catch()directive defines a statement block containing code for handling an exception thrown within the preceding @try block. The parameter of the @catch() directive is the exception object thrown locally, usually an NSException object. @finally directive defines a statement block (after the preceding@try block) that is subsequently executed whether or not the associated exception is thrown. The@finally block is commonly used to perform any cleanup actions for its corresponding @try and@catch() blocks (e.g., releasing resources, etc.). The syntax for code that uses the exception compiler directives is depicted in Listing 14-8.

Listing 14-8.  Exception Handling Using Compiler Directives

@try{  // Code implementing solution logic (may throw exceptions)  ...}@catch (NSException *exception){  // Exception handling code  ...}@finally{  // Cleanup code  ...}

The @throw directive is used to throw an exception, usually an NSException object but may, in fact, be other types of objects. When an exception is thrown within the body of a @try block, the program jumps immediately to the nearest @catch() block to handle the exception (if one exists).

The Cocoa frameworks come with a large set of predefined exception names that describe specific error states. These should be used (where applicable) when creating an exception.

原创粉丝点击