本文转载至 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. python-rtslib 模块

    Python library for configuring the Linux kernel-based multiprotocol SCSI target (LIO) A Python objec ...

  2. hdu 3061 hdu 3996 最大权闭合图 最后一斩

    hdu 3061 Battle :一看就是明显的最大权闭合图了,水提......SB题也不说边数多少....因为开始时候数组开小了,WA....后来一气之下,开到100W,A了.. hdu3996. ...

  3. 计蒜客 ACM竞赛高校联盟训练赛 第8场 煎牛排

    水一水. https://nanti.jisuanke.com/t/24205 煎牛排 题目描述 又是一个难得的周六,是时候远离食堂和外卖出去大吃一顿了.圈内知名吃货AA正想着中午去吃汉堡炸鸡烤肉火锅 ...

  4. 超实用的Nginx极简教程,覆盖了常用场景

    概述 安装与使用 安装 使用 nginx 配置实战 http 反向代理配置 负载均衡配置 网站有多个 webapp 的配置 https 反向代理配置 静态站点配置 搭建文件服务器 跨域解决方案 参考 ...

  5. 【lombok】lombok---帮你简化生成必要但臃肿的java代码工具 【映射注解和lombok注解同时使用 以及 映射注解放在属性和get方法上的区别】

    官方地址:https://projectlombok.org/ GitHub:https://github.com/rzwitserloot/lombok 指导说明文档:http://jnb.ociw ...

  6. java编程思想第四版第9章

    练习3: public class MainTest { public static void main(String args[]){ Bcycle b=new Bcycle(); b.print( ...

  7. python遍历当前目录并删除某文件

    #coding: utf-8 """ this programe is to clear driverlog below this dir __author__:the_ ...

  8. Xshell 初次应用

    以前就想安装Xshell,今天终于弄好了,可以在windows下对Linux服务端进行管理. 关于SSH和Xshell的介绍见参考,Linux上安装的是ssh服务端,所以咱们如果希望通过远程访问的方式 ...

  9. CentOS7设置DNS服务器

    CentOS7设置DNS服务器 在CentOS7下,手工设置 /etc/resolv.conf 里的DNS,过了一会,发现被系统重新覆盖或者清除了.CentOS7和CentOS6下的设置DNS方法不一 ...

  10. Android 使用意图播放本地视频

    Android播放视频的方式有三种: 一.使用意图播放,调用本地安装的播放器,选择一个进行播放. 二.使用VideoView播放(VideoView事实上是对MediaPlayer的封装,使用起来非常 ...