首页 / IOS / IOS开发之----异常处理
IOS开发之----异常处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了IOS开发之----异常处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5446字,纯文字阅读大概需要8分钟。
内容图文
本文转载至 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
原文:http://www.cnblogs.com/Camier-myNiuer/p/3825821.html
内容总结
以上是互联网集市为您收集整理的IOS开发之----异常处理全部内容,希望文章能够帮你解决IOS开发之----异常处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。