21st: Understanding the OBJECTIVE-C Error model

Source: Internet
Author: User

The first thing to note is:

" Auto Reference Count " (Automatic Reference counting, ARC, see 30th) is not " exceptionally safe " by default (Exception safe). Specifically, this means that if an exception is thrown, the object that should have been disposed at the end of the scope is now not automatically freed. If you want to generate "exception-safe" code, you can do this by setting the compiler's flags, but this introduces some extra code that will be executed even if the exception is not thrown. The compiler flag that needs to be opened is called -fobjc-arc-exceptions.

Even if you do not use arc, it is difficult to write code that does not cause a memory leak When an exception is thrown.

You should use exceptions whenever a critical error occurs that can cause the entire application to crash. After the exception is thrown, you do not have to consider the recovery issue, and the application should exit at this time. This means that you don't have to write complex "exception-safe" code anymore.

In the case of a less serious error, you can assign a delegate method to handle the error, or you can put the error message in the Nserror object and return it to the caller via an "output parameter" (delegate).

Or make the method return nil/0 to indicate that an error occurred.

3 messages are encapsulated in the nserror object:

1. Error domain (fault range, whose type is string)

The error range, which is the source of the error, is usually defined by a unique global variable. Example: Nsurlerrordomain to indicate an error range.

2. Error code (with an integer type)

Indicates that a series of related errors may occur within a particular scope, and these errors are usually defined by an enum.

Example: HTTP status code

3. User info (the type is a dictionary)

Additional information about the error, which may contain a "localized description" (localized description). There may also be another error that causes the error, and by this information, the related error can be strung into an " error chain " (Chain of Errors).

The nserror is often returned to the caller by an output parameter.

Example:-(BOOL) dosomething: (nserror**) error

The argument passed to the method is a pointer that itself points to another pointer, which points to the Nserror object. Or you can think of it as a pointer to a Nserror object directly.

In fact, when using arc, the compiler converts the nserror** in the method signature to nserror* __autoreleasing*, that is, the object that the pointer refers to is automatically freed after the method has finished executing. This object must be released on its own because the "dosomething:" method does not guarantee that its callers can release the Nserror created in this method, so they must join the autoreleass.

Nserror *error = nil; Output parameters

BOOL ret = [Object dosomething: &error];

if (Error) {

There is an error

*error = [Nserror errorwithdomain:domain code:code Userinfo:userinfo]; *error is the error parameter "dereference" (dereference). That is, the pointer that the error refers to is now pointing to a pro-Nserror object.

}

21st: Understanding the OBJECTIVE-C Error model

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.