NSError和NSException区别

来源:互联网 发布:java中最难的是什么 编辑:程序博客网 时间:2024/06/05 21:58

NSError和NSException区别

Exception是程序级别的错误,在catch的时候如果没有捕获会crash
Error是用户级别的错误

Exception:

NSException is used to implement exception handling and contains information about an exception. An exception is a special condition that interrupts the normal flow of program execution. Each application can interrupt the program for different reasons. For example, one application might interpret saving a file in a directory that is write-protected as an exception. In this sense, the exception is equivalent to an error. Another application might interpret the user’s key-press (for example, Control-C) as an exception: an indication that a long-running process should be aborted.

Two distinct types of problems can arise while an iOS or OS X application is running. Exceptions represent programmer-level bugs like trying to access an array element that doesn’t exist. They are designed to inform the developer that an unexpected condition occurred. Since they usually result in the program crashing, exceptions should rarely occur in your production code.

NSError

Every program must deal with errors as they occur at runtime. The program, for example, might not be able to open a file, or perhaps it cannot parse an XML document. Often errors such as these require the program to inform the user about them. And perhaps the program can attempt to get around the problem causing the error.

On the other hand, errors are user-level issues like trying load a file that doesn’t exist. Because errors are expected during the normal execution of a program, you should manually check for these kinds of conditions and inform the user when they occur. Most of the time, they should not cause your application to crash.

参考地址:
地址一:
http://rypress.com/tutorials/objective-c/exceptions
地址二:
http://stackoverflow.com/questions/11100951/nsexception-and-nserror-custom-exception-error

地址三 :
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSException_Class/index.html

地址四:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSError_Class/
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ErrorHandling/ErrorHandling.html#//apple_ref/doc/uid/TP40001806

0 0
原创粉丝点击