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 exception. The @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. A @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.
- Brief Intro to Exception Handling of Foundation Framework
- Brief Intro to Error handling of Foundation Framework
- Brief Intro to Strings of Foundation Framework
- Brief Intro to Notifications of Foundation Framework
- Brief Intro to NSError of Foundation Framework
- Brief Intro to NSException of Foundation Framework
- Brief Intro to URL Handling of Foundation Framework (Examples: Downloading from a URL)
- Brief Intro to Value Objects of Foundation Framework
- Brief Intro to Network Services of Foundation Framework
- Brief Intro to File System Utilities of Foundation Framework
- Brief Intro to Concurrency and Threading of Foundation Framework
- Brief Intro to Interprocess Communication of Foundation Framework
- Brief Intro to Archives and Serialization of Foundation Framework
- Brief Intro to Container Literals of Foundation Framework
- Brief Intro to Object Subscripting of Foundation Framework
- Brief Intro to Operations and Operation Queues of Foundation Framework
- Brief Intro to Collections of Foundation Frameworks
- Brief Intro to Foundation Functions
- C++内存管理
- 泰勒公式求e值——c语言算法实现
- UITabBarController
- socket通信实例server端
- socket通信实例client端
- Brief Intro to Exception Handling of Foundation Framework
- 【索引】线性表
- 冒泡和快速排序
- 验证数字的正则表达式集
- 《C语言深度解剖》笔记
- vim 编译备忘
- 并查集
- 【新浪100级博客地址】
- 【索引】二叉树