本文转载至 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. hdu3715 2-sat+二分

    Go Deeper 题意:确定一个0/1数组(size:n)使得满足最多的条件数.条件在数组a,b,c给出. 吐槽:哎,一水提,还搞了很久!关键是抽象出题目模型(如上的一句话).以后做二sat:有哪些 ...

  2. 链表ADT的实现

    list.h文件 /*链表的类型声明*/ typedef int ElementType; /* START: fig3_6.txt */ #ifndef _List_H #define _List_ ...

  3. cookie、session、localStorage、sessionStorage区别

    cookie.session 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份 ...

  4. 8大排序算法的java实现--做个人收藏

    排序算法分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因为数据量太大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.这里只讨论内部排序,常见的内部排序算法有:插入排序 ...

  5. 2018年东北农业大学春季校赛 F wyh的集合【思维】

    链接:https://www.nowcoder.com/acm/contest/93/F来源:牛客网 题目描述 你们wyh学长给你n个点,让你分成2个集合,然后让你将这n个点进行两两连接在一起,连接规 ...

  6. Akka之BackoffSupervisor

    一.背景 最近在开发一个项目,项目的各模块之间是使用akka grpc传输音频帧的,并且各模块中的actor分别都进行了persist.本周在开发过程中遇到了一个bug,就是音频帧在通行一段时间后,整 ...

  7. [置顶] python字典和nametuple互相转换例子

    如果tuple中的元素很多的时候操作起来就比较麻烦,有可能会由于索引错误导致出错. namedtuple对象给tuple命名. 下面的例子可以字典和nametuple互相转换 aa={'verbosi ...

  8. 2016.8.19 在dialog上增加一个button出现错误:failed to execute setAttribute on Element...

    目标:想要在dialog上多加一个button. 语法来自: http://api.jqueryui.com/dialog/#option-buttons   可见新增在dialog上的button要 ...

  9. 身份证实名认证接口调用实例(PHP)

    基于php的身份证实名认证接口调用代码实例,身份证实名认证接口申请:https://www.juhe.cn/docs/api/id/103 <!--?php // +-------------- ...

  10. apue学习笔记(第十一章 线程)

    本章将进一步深入理解进程,了解如何使用多个控制线程(简单得说就是线程)在单进程环境中执行多个任务. 线程概念 每个线程都包含有表示执行环境所必须的信息:线程ID.一组寄存器值.栈.调度优先级和策略.信 ...