NSException和NSError

NSException和NSError

很多语言将所有非预期(unexpected)错误作为异常抛出,但是Objective-C的异常只用来处理程序错误。当异常抛出时,详细信息都封装在NSException对象中。这些信息主要用来帮助程序员调试代码,例如“试图在只有两个对象的数组中访问第七个对象。”NSException中还包括方法调用栈信息,指明了抛出异常的代码位置。

NSException和NSError的使用场景不同。如果需要指出程序员的编码错误,则应该使用NSException。例如,一个方法只能接受奇数作为参数,但是程序员在调用该方法时传入了偶数,这时应该抛出异常,以方便程序员解决代码错误。相反,对于预期(expected)错误,如用户错误和设备环境错误,应该使用NSError。例如,一个方法需要读取用户照片,但是没有访问用户相册的权限,这时应该向方法调用者返回一个NSError对象,指出不能执行本次操作的原因。

时间: 2024-08-08 08:01:39

NSException和NSError的相关文章

Exceptions and Errors on iOS

https://blog.jayway.com/2010/10/13/exceptions-and-errors-on-ios/ October 13, 2010 by Fredrik Olsson in Architecture, Cocoa, Embedded, Testing, Tips & Tricks | 6 Comments Cocoa, and by inheritance Cocoa Touch on iOS makes a clear distinction between w

C以及Objective-C测试题讲解(下)

接前天的博客,今天继续讲解后27题.测试题地址:http://www.eosgarden.com/en/articles/objc-quizz/take/ 31.Which of the following can be inherited? 以下哪些可被继承? 答案:protocols,classes.协议和类 说明:要注意categories无法被继承. 32.How do you throw an exception? 如何抛出异常? 答案:@throw e 说明:Object-C语言的异

【HELLO MAKA】MAKA iOS客户端 之二 架构设计与实现篇

上一篇主要做了MAKA APP的需求分析,功能结构分解,架构分析,API分析,API数据结构分析. 这篇主要讲如何从零做iOS应用架构. [HELLO MAKA]MAKA iOS客户端 之一 APP分析篇 [HELLO MAKA]MAKA iOS客户端 之二 架构设计与实现篇 [HELLO MAKA]MAKA iOS客户端 之三 创作模块分析与实现篇 1.  iOS客户端架构 按照功能模块划分.这里可以使用二层设计也可以使用三层设计.MVC, MVCS, MVVM, MVP, VIPER, DD

9.块

原文:http://rypress.com/tutorials/objective-c/blocks Blocks 块 块是OC的匿名函数.块特性使得能够在不同类之前传递某段代码(函数),这比去调用某个地方定义的某个方法更加直观,另外由于块是封闭的,所以可以排除外部干扰. 创建块 块是基于函数的.你可以申明一个块就像声明一个函数一样,实现一个块和实现一个函数基本上没有区别. // main.m #import <Foundation/Foundation.h> int main(int arg

IOS开发之----异常处理

本文转载至 http://blog.csdn.net/chenyong05314/article/details/7906593 转载自:http://blog.sina.com.cn/s/blog_71715bf8010166qf.html 开篇大话: Object-C语言的异常处理符号和C++.JAVA相似.再加上使用NSException,NSError或者自定义的类,你可以在你的应用程序里添加强大的错误处理机制.异常处理机制是由这个四个关键字支持的:@try,@catch,@thorw,

IOS Exception 1(libc++abi.dylib: terminating with uncaught exception of type NSException)

2014-08-05 22:18:46.455 SwiftUI[1329:40871] -[_TtC7SwiftUI14MViewControler clickMe]: unrecognized selector sent to instance 0x10ea15dc0 2014-08-05 22:18:46.458 SwiftUI[1329:40871] *** Terminating app due to uncaught exception 'NSInvalidArgumentExcept

ios 自定义NSError

from:[object-c错误处理]http://www.androiddev.net/objective-c%E5%AD%A6%E4%B9%A0%E4%B9%8B%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86/ NSString *domain = @”com.MyCompany.MyApplication.ErrorDomain”;NSString *desc = NSLocalizedString(@”Unable to…”, @””);NSDictionary

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 pe

ios错误码:NSError对象.code

1. URL Loading System Error Codes These values are returned as the error code property of an NSError object with the domain “NSURLErrorDomain”. enum { NSURLErrorUnknown = -1, NSURLErrorCancelled = -999, NSURLErrorBadURL = -1000, NSURLErrorTimedOut =