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?
我最近开始学习Objective-C,我正在开发一个iOS应用程序作为练习,无论如何,我想通过抛出异常处理溢出(我来自Java背景),我搜索引用只有NSException,但是我在阅读有关异常处理主题的部分中读到,并且他们说使用NSError,我读了参考但他们有相同的协议和方法,那么它们之间的区别是什么?哪个更好?
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
另外,我想创建自己的异常或错误类,是否应该包含任何方法或字段? (就像在Java中实现Exception接口时一样)。谢谢
1 个解决方案
#1
22
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).
NSError专为非致命,可恢复的错误而设计。设计为由NSError捕获的问题通常是用户错误(或者是可以呈现给用户的错误),通常可以从(因此-presentError:和NSErrorRecoveryAttempting)中恢复,并且通常是预期的或可预测的错误(比如试图打开一个你无权访问的文件,或尝试在不兼容的字符串编码之间进行转换。
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.
NSException是针对可能致命的程序员错误而设计的。这些错误旨在表示您的应用程序中的潜在缺陷,其中您没有正确检查执行某些操作的前提条件(例如尝试访问超出其范围的数组索引,或尝试改变不可变对象)。 “异常编程指南”的介绍解释了这一点。
#1
22
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).
NSError专为非致命,可恢复的错误而设计。设计为由NSError捕获的问题通常是用户错误(或者是可以呈现给用户的错误),通常可以从(因此-presentError:和NSErrorRecoveryAttempting)中恢复,并且通常是预期的或可预测的错误(比如试图打开一个你无权访问的文件,或尝试在不兼容的字符串编码之间进行转换。
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.
NSException是针对可能致命的程序员错误而设计的。这些错误旨在表示您的应用程序中的潜在缺陷,其中您没有正确检查执行某些操作的前提条件(例如尝试访问超出其范围的数组索引,或尝试改变不可变对象)。 “异常编程指南”的介绍解释了这一点。