Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSException and NSError custom exception/error

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

like image 393
hakuna matata Avatar asked Jun 19 '12 12:06

hakuna matata


People also ask

What is NSException?

An object that represents a special condition that interrupts the normal flow of program execution.

How to handle exception in Objective C?

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.

What is made up of NSError object?

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.

What is exception handling in asp net?

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.


1 Answers

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.

like image 131
一二三 Avatar answered Oct 06 '22 01:10

一二三