Brief Intro to Error handling of Foundation Framework

Runtime Error Conditions

runtime error is an error that occurs while a program is running; it contrasts with other categories of program errors, such as syntax and linking errors, which occur prior to program execution. Runtime errors are generally due to the following types of conditions:

  • Logical error: An error due to the fact that the code doesn’t correctly implement the corresponding program logic. This type of error is manifested during program execution when it doesn’t produce the expected output.
  • Semantic error: An error due to the improper use of program statements (for example, performing division by zero or the improper initialization of a variable).
  • User input error: An error due to invalid user input.

If any of these conditions occurs and is not handled, it can result in a variety of adverse consequences, including program termination or improper operation. As a result, a variety of mechanisms have been developed in computer programming to detect and respond to errors. ForObjective-C, the Foundation Framework includes several APIs for error handling, grouped into the following categories:

  • Assertions
  • Error codes
  • Error objects
  • Exceptions

Before examining these APIs in detail, let’s briefly review each of these error types and how they are used.

Assertions

An assertion is used to provide a runtime check of a condition that, if not present, should cause the program to terminate. Assertions can be disabled via a compiler directive; hence, they should only be used to detect programming logic and semantic errors, not runtime errors due to invalid user input, and so forth. In Chapter 13, you learned about the Foundation Framework assertion functions. These support the creation of assertions for both Objective-C methods and functions.

Error Codes

Error codes are unique integer values used to identify and perhaps convey information about a particular error that occurs during program execution. Error codes are a simple mechanism for reporting errors but are limited in the amount of information they can provide for error handling.

Error Objects

Error objects are used to both encapsulate and convey information about runtime errors that users need to know about. An error object is made up of an error code and additional information related to the error, some of which can potentially be used for recovering from the error. Because they are objects, error objects also benefit from the basic properties of OOP (encapsulation, inheritance, subclassing, etc.). The Foundation Framework includes several APIs to support error objects: theNSError class for creating and manipulating error objects, and a set of standard error codes.

Exceptions

In computer programming, an exception is an anomalous, unexpected event that occurs during program execution and requires special processing. Exception handling typically changes program flow, with control being passed to special code designated for this purpose. Exception processing logic varies depending upon the severity of the event, ranging from simple logging to attempting recovery and to program termination. The Foundation Framework includes several APIs to support exception processing: the NSException class for creating and manipulating exceptions, and a set of standard exception names. The Objective-C language includes a set of directives that are used with the Foundation Framework exception APIs to perform exception processing.




Error Handling Guidelines

Error objects should be used to detect and handle expected errors within an Objective-C program that can (potentially) be recovered from. Assertions should be used as a programming aid to check program invariants, conditions, assumptions, and so forth. Note that because assertions may be disabled prior to compilation (as is often the case for production releases), they should not be used for parameter checking of public APIs. Exceptions should only be used to detect programming or unexpected runtime errors that cannot be recovered from, and which should be handled by immediately terminating the application.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值