I recently started learning Objective-C, and I am working on an iOS app as an exercise, anyway, I want to handle overflow by throwing exception (I come from a Java background), I searched the reference there is only NSException
, but then I read in the section that say topics about exception handling, and they said to use NSError
, I read the reference but they had the same protocol and methods, so what's the difference between them? And which is better?
Also, I want to create my own exception or error class, are there any methods or fields that I should include? (Like when implementing the Exception
interface in Java).
Thanks
An object that represents a special condition that interrupts the normal flow of program execution.
The exception handling mechanisms available to Objective-C programs are effective ways of dealing with exceptional conditions. They decouple the detection and handling of these conditions and automate the propagation of the exception from the point of detection to the point of handling.
The core attributes of an NSError object are an error domain (represented by a string), a domain-specific error code and a user info dictionary containing application specific information.
Exception handling is an in-built mechanism in . NET Framework to detect and handle run time errors. Exceptions are defined as anomalies that occur during the execution of a program. The . NET Framework provides a rich set of standard exceptions that are used during exceptions handling.
NSError
is designed for non-fatal, recoverable errors. The problems that are designed to be captured by an NSError
are often user errors (or are errors that can be presented to the user), can often be recovered from (hence -presentError:
and NSErrorRecoveryAttempting
), and are usually expected or predictable errors (like trying to open a file that you don't have access to, or trying to convert between incompatible string encodings).
NSException
is designed for potentially fatal, programmer errors. These errors are designed to signify potential flaws in your application where you have not correctly checked the pre-conditions for performing some operations (like trying to access an array index that is beyond its bounds, or attempts to mutate an immutable object). The introduction to the Exception Programming Guide explains this a little bit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With