本文转载至 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,@finally。当代码有可能出现异常时,我们把他放到@try语句块中。@catch()块包含了处理@try块里的抛出的异常的逻辑。无论异常是否发生,@finally块里面的语句都会执行。如果直接使用@throw块来抛出异常,这个异常本质上是一个OC的对象。咱们可以使用NSException对象,但是不局限于他们。

Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。

异常处理捕获的语法:

  • @try {
  • <#statements#>
  • }
  • @catch (NSException *exception) {
  • <#handler#>
  • }
  • @finally {
  • <#statements#>
  • }

@catch{} 块 对异常的捕获应该先细后粗,即是说先捕获特定的异常,再使用一些泛些的异常类型。

我们自定义两个异常类,看看异常异常处理的使用。

1、新建SomethingException,SomeOverException这两个类,都继承与NSException类。

SomethingException.h

  • #import <Foundation/Foundation.h>
  • @interface SomethingException : NSException
  • @end

SomethingException.m

  • #import "SomethingException.h"
  • @implementation SomethingException
  • @end

SomeOverException.h

  • #import <Foundation/Foundation.h>
  • @interface SomeOverException : NSException
  • @end

SomeOverException.m

  • #import "SomeOverException.h"
  • @implementation SomeOverException
  • @end

2、新建Box类,在某些条件下产生异常。

  • #import <Foundation/Foundation.h>
  • @interface Box : NSObject
  • {
  • NSInteger number;
  • }
  • -(void) setNumber: (NSInteger) num;
  • -(void) pushIn;
  • -(void) pullOut;
  • -(void) printNumber;
  • @end
  • @implementation Box
  • -(id) init {
  • self = [super init];
  • if ( self ) {
  • [self setNumber: 0];
  • }
  • return self;
  • }
  • -(void) setNumber: (NSInteger) num {
  • number = num;
  • if ( number > 10 ) {
  • NSException *e = [SomeOverException
  • exceptionWithName: @"BoxOverflowException"
  • reason: @"The level is above 100"
  • userInfo: nil];
  • @throw e;
  • else if ( number >= 6 ) {
  • // throw warning
  • NSException *e = [SomethingException
  • exceptionWithName: @"BoxWarningException"
  • reason: @"The level is above or at 60"
  • userInfo: nil];
  • @throw e;
  • else if ( number < 0 ) {
  • // throw exception
  • NSException *e = [NSException
  • exceptionWithName: @"BoxUnderflowException"
  • reason: @"The level is below 0"
  • userInfo: nil];
  • @throw e;
  • }
  • }
  • -(void) pushIn {
  • [self setNumber: number + 1];
  • }
  • -(void) pullOut {
  • [self setNumber: number - 1];
  • }
  • -(void) printNumber {
  • NSLog(@"Box number is: %d", number);
  • }
  • @end

这个类的作用是,初始化Box时,number数字是0,可以用pushIn 方法往Box里推入数字,每调用一次,number加1.当number数字大于等于6时产生SomethingException异常,告诉你数字达到或超过6了,超过10时产生SomeOverException异常,小于1时产生普通的NSException异常。

这里写 [SomeOverException  exceptionWithName:@"BoxOverflowException"  reason:@"The level is above 100"异常的名称和理由,在捕获时可以获取。

3、使用Box,在适当添加下捕获Box类的异常

3.1、在没超过6时,没有异常

  • - (void)viewDidLoad
  • {
  • [super viewDidLoad];
  • NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  • Box *box = [[Box alloc]init];
  • for (int i = 0; i < 5; i++) {
  • [box pushIn];
  • [box printNumber];
  • }
  • }

打印结果:

Box number is: 1

Box number is: 2

Box number is: 3

Box number is: 4

Box number is: 5

3.2 超过6,产生异常

  • for (int i = 0; i < 11; i++) {
  • [box pushIn];
  • [box printNumber];
  • }
  • 2012-07-04 09:12:05.889 ObjectiveCTest[648:f803] Box number is: 1
  • 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 2
  • 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 3
  • 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 4
  • 2012-07-04 09:12:05.891 ObjectiveCTest[648:f803] Box number is: 5
  • 2012-07-04 09:12:05.891 ObjectiveCTest[648:f803] *** Terminating app due to uncaught exception 'BoxWarningException', reason: 'The number is above or at 60'

这是时,程序抛出异常崩溃了。那怎么使程序不崩溃呢,做异常处理。

3.3、加上异常处理

  • for (int i = 0; i < 11; i++) {
  • @try {
  • [box pushIn];
  • }
  • @catch (SomethingException *exception) {
  • NSLog(@"%@ %@", [exception name], [exception reason]);
  • }
  • @catch (SomeOverException *exception) {
  • NSLog(@"%@", [exception name]);
  • }
  • @finally {
  • [box printNumber];
  • }
  • }

运行,程序没有崩溃,打印结果:

  • 2012-07-04 09:14:35.165 ObjectiveCTest[688:f803] Box number is: 1
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 2
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 3
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 4
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 5
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 6
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 7
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 8
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 9
  • 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 10
  • 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxOverflowException
  • 2012-07-04 09:14:35.225 ObjectiveCTest[688:f803] Box number is: 11

超过10时,SomeOverException异常抛出。

3.4 、小于0时的异常

在Box类的setNumber里,当number小于0时,我们抛出普通异常。

  • @try {
  • [box setNumber:-10];
  • }
  • @catch (NSException *exception) {
  • NSLog(@"%@",[exception name]);
  • }
  • @finally {
  • [box printNumber];
  • }

打印结果:

  • 2012-07-04 09:17:42.405 ObjectiveCTest[753:f803] BoxUnderflowException
  • 2012-07-04 09:17:42.406 ObjectiveCTest[753:f803] Box number is: -10

IOS开发之----异常处理的更多相关文章

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

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

  2. iOS开发系列-异常处理

    概述 在开发中经常调用苹果的API遇到数组越界.实例方法不存在运行时等致命错误,此时程序直接奔溃.其实苹果是在函数内部抛出了一个异常.这样告诉开发者需要检查代码做修改.同样在我们自己封装一些框架或者功 ...

  3. iOS开发——新特性Swift篇&Swift 2.0 异常处理

    Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...

  4. iOS开发-多线程开发之线程安全篇

    前言:一块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源,比如多个线程访问同一个对象.同一个变量.同一个文件和同一个方法等.因此当多个线程访问同一块资源时,很容易会发生数据错误及数据不安 ...

  5. iOS开发系列--Swift语言

    概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...

  6. iOS开发系列--打造自己的“美图秀秀”

    --绘图与滤镜全面解析 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益于它强大的开发框架.今天我们将围绕iOS中两大图形.图像绘图框架进行介绍:Quartz ...

  7. iOS开发之再探多线程编程:Grand Central Dispatch详解

    Swift3.0相关代码已在github上更新.之前关于iOS开发多线程的内容发布过一篇博客,其中介绍了NSThread.操作队列以及GCD,介绍的不够深入.今天就以GCD为主题来全面的总结一下GCD ...

  8. 总结iOS开发中的断点续传那些事儿

    前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...

  9. iOS开发系列文章(持续更新……)

    iOS开发系列的文章,内容循序渐进,包含C语言.ObjC.iOS开发以及日后要写的游戏开发和Swift编程几部分内容.文章会持续更新,希望大家多多关注,如果文章对你有帮助请点赞支持,多谢! 为了方便大 ...

随机推荐

  1. Topcoder SRM 664 DIV 1

    BearPlays 快速幂 题意: 给你两个数A,B,有种操作是将大的数减去小的数,并将小的数乘以2.反复k次,问你最后的小的数回是多少. 题解: 由于整个过程$A+B$的值是不会改变的.现在令$S= ...

  2. luogu P1489 猫狗大战

    题目描述 新一年度的猫狗大战通过SC(星际争霸)这款经典的游戏来较量,野猫和飞狗这对冤家为此已经准备好久了,为了使战争更有难度和戏剧性,双方约定只能选择Terran(人族)并且只能造机枪兵. 比赛开始 ...

  3. luogu P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  4. Cygwin下vi和vim方向键和Backspace不好用的问题(转)

    执行p4 client时vi打开文本,编辑起来恼火,于是找到了这个: 默认的vi(vim)没有配置文件,因此导致了方向键出现ABCD,以及Backspace只会移动光标,字符不消失的问题.cygwin ...

  5. 8 Most Required Examples Reference For Oracle Forms

    Check the following 8 Links for best Oracle Forms examples with source code (Fmb files), which will ...

  6. HDU1087上升子序列的最大和

     解法一 此题是一个简单的动态规划问题,用dp[i]记做最后一步经过第i个数所得到的最大sum值,则结果=max(dp[i]),i=1,...n.考虑dp[i]的前一步会经过那里?假设dp[i]的前一 ...

  7. VirtualBox导入XXXX.vdi时报错

    virtualbox导入vdi文件时出现以下的问题: 解决方法: windows+R,输入cmd,进入virtualbox的安装文件夹(或者在硬盘中直接进入virtualbox的安装文件夹.在任务栏里 ...

  8. 2016.7.12 在navicat中用sql语句建表

    参考资料: http://jingyan.baidu.com/article/f0e83a25a8c4b022e5910116.html 即新建query,然后run. (1)点击要新建表的位置,选择 ...

  9. ES6 rest与扩展运算符

    1.rest 变量将多余的参数放入数组中. function add(...values) { let sum = 0; for (var val of values) { sum += val; } ...

  10. 理解支持向量机(三)SMO算法

    在支持向量机模型的求解中,我们用到了SMO算法来求解向量α. 那么什么是SMO算法?在讲SMO算法之前.我们须要先了解下面坐标上升法. 1.坐标上升法 如果有优化问题: W是α向量的函数.利用坐标上升 ...