문제

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.

도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top