Question

Which one is the most preferred objective C exception handling ? NSError pointer which give the pointer with message details within it or @throws which forces the caller to handle exception and show some graceful message. Thanks.

and alos please let me know which one is memory efficient.

Était-ce utile?

La solution

Unlike Java, in Objective-C exceptions are just used to handle unrecoverable states. This means that for example, you don't catch EXC_BAD_ACCESS, rather you debug your application and correct the problem.

The Objective-C equivalent of Java exceptions is NEError and the null pointer pattern. For example if a formatter fails to format a string to a number because the string does not represent a number, it returns nil. There is also a method to format the number that takes a NSError double pointer to return the error description.

So to handle errors you typically implement a method like this:

- (id) object : (out NSError**) error
{
    BOOL ok= ... 
    if(!ok)
    {
        if(error)
            *error= ...
        return nil;
    }
    return someObject;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top