异常处理是管理非典型事件(例如未被识别的消息)的过程,此过程将会中断正常的程序执行。如果没有足够的错误处理,遇到非典型事件时,程序可能立刻抛出(或者引发)一种被称之为异常的东西,然后结束运行。

异常的类型

程序抛出异常的原因多种多样,可由硬件导致也可由软件引起。异常的例子很多,包括被零除、下溢和上异之类的数学错误,调用未定义的指令(例如,试图调用一个没有定义的方法 )以及试图越界访问群体中的元素 。

Cocoa异常由NSException对象作为载体,下面是NSException的声明:

1 @interface NSException : NSObject <NSCopying, NSCoding> {
2 @private
3 NSString *name;
4 NSString *reason;
5 NSDictionary *userInfo;
6 id reserved;
7 }
  • name — a short string that is used to uniquely identify the exception. The name is required.

  • reason — a longer string that contains a “human-readable” reason for the exception. The reason is required.

  • An optional dictionary (userInfo) used to supply application-specific data to the exception handler. For example, if the return value of a method causes an exception to be raised, you could pass the return value to the exception handler through userInfo.

你可以在异常捕获后提取有用的信息,还可以适当的弹出一个异常警告框。

抛出的异常必须是NSException或者其子类,不能是其他类。

下面提供示例代码:

 1     NSException* ex = [[NSException alloc] initWithName:@"ExceptionName"   // just for test
2 reason:@"XXX"
3 userInfo:nil];
4
5 CustomNSException* ex = [[CustomNSException alloc] initWithName:@"CustomNSExceptionName" // just for test
6 reason:@"XXX"
7 userInfo:nil];
8
9 @try {
10 bool error = YES;
11 if (error) {
12 @throw ex;
13 }
14 }
15
16 @catch ( CustomNSException *exception ) {
17 NSLog(@"CustomNSException.name = %@" , CustomNSException.name);
18 NSLog(@"CustomNSException.reason = %@" , CustomNSException.reason);
19
20 // 弹出警告框,提示异常信息
21 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:CustomNSException.name
22 message:CustomNSException.reason
23 delegate:nil
24 cancelButtonTitle:nil
25 otherButtonTitles:nil];
26
27 [alert show];
28 [alert release];
29 }
30
31 @catch ( NSException *exception ) {
32 NSLog(@"exception.name = %@" , exception.name);
33 NSLog(@"exception.reason = %@" , exception.reason);
34 }
35
36 @finally {
37 NSLog(@"@finally");
38 }

NSException异常处理的更多相关文章

  1. 从C#到Objective-C,循序渐进学习苹果开发(4)--代码块(block)和错误异常处理的理解

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本文继续上一篇随笔<从 ...

  2. 浅谈Objective-C异常处理

    -----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...

  3. IOS开发之--异常处理--使用try 和 catch 来捕获错误。

    一个搞java的老板问我会不会try catch  我说不会 学这么久也没听周围朋友用这个 因为苹果控制台本来就可以打印异常 特此研究一下. 1.try catch:  是捕获异常代码段   特点:对 ...

  4. OC学习8——异常处理

    1.和Java一样,OC也有自己的一套异常处理机制,不同的是,OC中的异常处理机制并不是作为常规的编程实践,通常只是作为一种程序调试.排错机制. 2.与Java中类似,OC中也是采用@try...@c ...

  5. 【IOS 开发】Objective - C 面向对象高级特性 - 包装类 | 类处理 | 类别 | 扩展 | 协议 | 委托 | 异常处理 | 反射

    一. Objective-C 对象简单处理 1. 包装类 (1) 包装类简介 NSValue 和 NSNumber : -- 通用包装类 NSValue : NSValue 包装单个 short, i ...

  6. object-c的异常处理机制

    转载请注明:http://blog.sina.com.cn/s/blog_69081e060100utl5.html   一直听说iOS有异常处理机制,却从来没有关系过,今天小生就来关心下iOS的异常 ...

  7. iOS捕获异常,常用的异常处理方法

    本文转载至 http://www.cocoachina.com/ios/20141229/10787.html 前言:在开发APP时,我们通常都会需要捕获异常,防止应用程序突然的崩溃,防止给予用户不友 ...

  8. Objective-C try/catch异常处理机制原理。

    try-catch-finaly finally在任何情况下都会执行(不管有没有异常),属于整个体系的附属. 基本思想是跳到捕获锚点,重新执行. http://www.cnblogs.com/mark ...

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

    本文转载至 http://blog.csdn.net/chenyong05314/article/details/7906593 转载自:http://blog.sina.com.cn/s/blog_ ...

随机推荐

  1. WebDriver(Selenium2) 判断页面是否刷新的方法

    http://uniquepig.iteye.com/blog/1568208 public static boolean waitPageRefresh(WebElement trigger) { ...

  2. 安卓图表引擎AChartEngine(四) - 源码示例 嵌入Acitivity中的折线图

    前面几篇博客中都是调用ChartFactory.get***Intent()方法,本节讲的内容调用ChartFactory.get***View()方法,这个方法调用的结果可以嵌入到任何一个Activ ...

  3. mytop

    http://jeremy.zawodny.com/mysql/mytop/mytop.html 内容有点过时,用?查看帮助 vi set nuset nonu总记不住

  4. CodeForces 609C Load Balancing

    先算出目标状态,然后拿当前状态与目标状态对比,即可算出答案 #include<cstdio> #include<cmath> #include<cstring> # ...

  5. (五)Jquery Mobile列表

    Jquery Mobile列表 一.JM列表 1.普通列表            效果:            带序号的列表 将ul换成ol      效果:       2.data-inset=& ...

  6. CentOS 6.4 x64 安装 配置 Redmine 2.4.1

    Redmine 安装配置 1. 安装Redmine 所需的依赖 首先安装 yaml wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz ...

  7. 神经网络 Neuroph - Java Neural Network Platform Neuroph

    http://neuroph.sourceforge.net/image_recognition.html https://github.com/neuroph/neuroph

  8. [iOS Animation]-CALayer 性能优化

    性能优化 代码应该运行的尽量快,而不是更快 - 理查德 在第一和第二部分,我们了解了Core Animation提供的关于绘制和动画的一些特性.Core Animation功能和性能都非常强大,但如果 ...

  9. CodeForces 626C Block Towers

    构造AC的.左右两边都先不用6的倍数,然后哪边数字大那一边往回退一下,然后再比较哪边数字大.......直到结束 #include<cstdio> #include<cstring& ...

  10. CodeForces 626B Cards

    瞎搞题...凭直觉+猜测写了一发,居然AC了.. #include<cstdio> #include<cstring> #include<cmath> #inclu ...