Objective-C语法之异常处理
2013-08-06 09:39
393 查看
Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。
异常处理捕获的语法:
@catch{} 块 对异常的捕获应该先细后粗,即是说先捕获特定的异常,再使用一些泛些的异常类型。
我们自定义两个异常类,看看异常异常处理的使用。
SomethingException.m
SomeOverException.h
SomeOverException.m
这个类的作用是,初始化Box时,number数字是0,可以用pushIn 方法往Box里推入数字,每调用一次,number加1.当number数字大于等于6时产生SomethingException异常,告诉你数字达到或超过6了,超过10时产生SomeOverException异常,小于1时产生普通的NSException异常。
这里写 [SomeOverException exceptionWithName:@"BoxOverflowException" reason:@"The level is above 100"异常的名称和理由,在捕获时可以获取。
转载地址:http://blog.csdn.net/totogo2010/article/details/7713793
异常处理捕获的语法:
@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]; } }
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]; } }
3.4 、小于0时的异常
在Box类的setNumber里,当number小于0时,我们抛出普通异常。@try { [box setNumber:-10]; } @catch (NSException *exception) { NSLog(@"%@",[exception name]); } @finally { [box printNumber]; }
转载地址:http://blog.csdn.net/totogo2010/article/details/7713793
相关文章推荐
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- 6、Objective-C语法之异常处理
- Objective-C语法之异常处理
- 5,Objective-C语法之异常处理
- objective-c语法学习:异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C中使用@try处理异常
- Objective-C:异常的处理
- Objective-C中异常处理
- c/objective-c/win32/mfc/qt 异常处理与总结
- Objective-C 编程语言(11)异常处理----使能异常处理、异常处理
- c/objective-c/win32/mfc/qt 异常处理与总结
- Python基础语法(3)——文件流、异常处理、面向对象编程以及装饰器
- python语法31[异常处理]
- 读取文件、OS模块、异常处理、with as语法
- objective-c异常处理机制详解
- Java高级语法笔记-普通异常处理