Question

Is there a good way to deal with NSError in ios development?I was going trough some online references and i found that most of them were using [NSApp presentError] to present the error??

This is definitely not possible in ios as there is nothing called NSApp in the ios realm..

I was wondering how should one go about handling NSErrors other than just printing what

the error really is using [error localizedDescription]

Should one access the error code property of the NSError class and handle all the possible

error codes that one might expect in a particular situation using an else if/ladder??

**It would also be helpful if one could explain the major difference between NSError and

NSException? an why we have two seperate thing at the first place**

Was it helpful?

Solution

An NSError is a return code, where you tried to do something, but what you tried didn't work. For example, you tried to open a file, but it wasn't there. An NSError indicates an error condition where, with the same call on a different set of circumstances, you might not get an error (the file is there). An NSException indicates something fatally bad. You tried to write to null, you tried to write past the end of a string or an array. NSExceptions should be cleaned up by you, the programmer, NSErrors may just be happenstance errors.
What you do with an NSError is dependent on context. Sometimes you want to notify the user with an alert, sometimes you want to ignore it (like you test for a resource that's not there before you write a new one). Probably at the very least, you want to NSLog your NSErrors (given that you use a macro to strip NSLogs out of your final code).

OTHER TIPS

Whatever you do with NSErrors depends on your particular application, just use common sense.

Unlike other languages like Java exceptions are resource-intensive in Objective-C. Therefore,

  • Use exceptions to indicate non-recoverable program errors. The program catching such an exception should quit soon afterwards. Frameworks are not designed to let you recover from exceptions.

  • Don't use exceptions to indicate user errors, recoverable errors, or for general flow-control. These cases should handled with NSError instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top