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,@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开发之----异常处理的更多相关文章
- IOS开发之--异常处理--使用try 和 catch 来捕获错误。
一个搞java的老板问我会不会try catch 我说不会 学这么久也没听周围朋友用这个 因为苹果控制台本来就可以打印异常 特此研究一下. 1.try catch: 是捕获异常代码段 特点:对 ...
- iOS开发系列-异常处理
概述 在开发中经常调用苹果的API遇到数组越界.实例方法不存在运行时等致命错误,此时程序直接奔溃.其实苹果是在函数内部抛出了一个异常.这样告诉开发者需要检查代码做修改.同样在我们自己封装一些框架或者功 ...
- iOS开发——新特性Swift篇&Swift 2.0 异常处理
Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...
- iOS开发-多线程开发之线程安全篇
前言:一块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源,比如多个线程访问同一个对象.同一个变量.同一个文件和同一个方法等.因此当多个线程访问同一块资源时,很容易会发生数据错误及数据不安 ...
- iOS开发系列--Swift语言
概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...
- iOS开发系列--打造自己的“美图秀秀”
--绘图与滤镜全面解析 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益于它强大的开发框架.今天我们将围绕iOS中两大图形.图像绘图框架进行介绍:Quartz ...
- iOS开发之再探多线程编程:Grand Central Dispatch详解
Swift3.0相关代码已在github上更新.之前关于iOS开发多线程的内容发布过一篇博客,其中介绍了NSThread.操作队列以及GCD,介绍的不够深入.今天就以GCD为主题来全面的总结一下GCD ...
- 总结iOS开发中的断点续传那些事儿
前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...
- iOS开发系列文章(持续更新……)
iOS开发系列的文章,内容循序渐进,包含C语言.ObjC.iOS开发以及日后要写的游戏开发和Swift编程几部分内容.文章会持续更新,希望大家多多关注,如果文章对你有帮助请点赞支持,多谢! 为了方便大 ...
随机推荐
- 关于expect脚本输出的问题
写了一个expect脚本 执行ssh命令远程登录 然后telnet另外一台机器 大致如下: #!/usr/bin/expect -f set timeout set port_type [lindex ...
- springBoot 数组增加工具类包
1.pom中加入依赖 <!--数组工具类 start--> <dependency> <groupId>org.apache.commons</groupId ...
- ORACLE的字符串操作函数
字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类 ...
- codevs_1043 方格取数(棋盘DP)
1043 方格取数 2000年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description ...
- hosts文件中同一个域名两个IP的解析顺序
比如: 192.168.0.2 www.easonjim.com 192.168.0.3 www.easonjim.com 那么解析顺序就只有最开头的IP,即:192.168.0.2 经过测试,相同域 ...
- git常用命令,制作缩写命令
目录 基础命令 常用命令列表 查看状态 添加到本地仓库 推送到远程仓库 创建分支 更新分支, 合并分支 查看分支的差异 回滚 其它 缩写命令 基础命令 # 生成SSH key ssh-keygen - ...
- Android 分享透明图片到微信变黑的问题
/** * bitmap中的透明色用白色替换 * * @param bitmap * @return */ public static Bitmap changeColor(Bitmap bitmap ...
- JAVAWEB开发之JSP、EL、及会话技术(Cookie和Session)的使用详解
Servlet的缺点 开发人员要十分熟悉JAVA 不利于页面调试和维护(修改,重新编译) 很难利用网页设计工具进行页面设计(HTML内容导入到servlet中,用PrintWriter的对象进行输出) ...
- 关闭火狐定期向“http://detectportal.firefox.com/”发包
问题:最近发现火狐总是向http://detectportal.firefox.com/发包 办法:firefox地址栏输入 about:config,然后搜索找到 network.captive-p ...
- EasyMvc入门教程-图形控件说明(21)线形图+柱状图+饼形图
本章将介绍一些基本但常用的图形:线型图,柱状图和饼形图. 以上三种图形对于的数据都是键值对数组,请参考第一个例子: @{ var data = new List<LineItem>(); ...