Objective-C语法之异常处理
来源:互联网 发布:手机淘宝申请代付 编辑:程序博客网 时间:2024/05/16 11:01
@try { <#statements#> } @catch (NSException *exception) { <#handler#> } @finally { <#statements#> }
1、新建SomethingException,SomeOverException这两个类,都继承与NSException类。
#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
@implementation SomeOverException
2、新建Box类,在某些条件下产生异常。
#import <Foundation/Foundation.h>@interface Box : NSObject{ NSInteger number;}-(void) setNumber: (NSInteger) num;-(void) pushIn;-(void) pullOut;-(void) printNumber;@end
-(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"
@throw e; }}
[self setNumber: number + 1];
}
-(void) pullOut {
[self setNumber: number - 1];
}
-(void) printNumber {
NSLog(@"Box number is: %d", number);
}
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
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.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
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
3.4 、小于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
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- 5,Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- 6、Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective C 语法系列--异常处理
- objective-c语法学习:异常处理
- LHF Objective-C语法(5)异常处理
- Objective-c异常处理
- objective-c 异常处理
- objective-c中的异常处理
- Objective-C - 异常处理(Exception)
- hdoj 1019 A Mathematical Curiosity【基础题】
- redis,mongodb,hbase,mola,aerospike,couchbase,对比
- Nginx之location 匹配规则详解
- Unity游戏开发使用Assetbundle加载场景的原理
- 格子架!~
- Objective-C语法之异常处理
- Java:方法的虚分派(virtual dispatch)和方法表(method table)
- SVN图形化界面工具Cornerstone的管理项目
- 惠普培训随笔
- 小白笔记-------------------------vim安装
- 分层K-Means+Matlab计算向量间距离
- XMPP框架 微信项目开发之XMPP配置(二)——安装配置客户端(Adium)和配置系统自带的信息程序,并对聊天进行测试
- C语言版Tokenize()函数,由分隔符获取字符串
- NFS网络文件系统共享日志