目录
(?)
Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。
异常处理捕获的语法:
- @try{
- <#statements#>
- }
- @catch(NSException*exception){
- <#handler#>
- }
- @finally{
- <#statements#>
- }
我们自定义两个异常类,看看异常异常处理的使用。
1、新建SomethingException,SomeOverException这两个类,都继承与NSException类。
SomethingException.h
- #import<Foundation/Foundation.h>
- @interfaceSomethingException:NSException
- @end
- #import"SomethingException.h"
- @implementationSomethingException
- @end
- #import<Foundation/Foundation.h>
- @interfaceSomeOverException:NSException
- @end
- #import<Foundation/Foundation.h>
- @interfaceBox:NSObject
- {
- NSIntegernumber;
- }
- -(void)setNumber:(NSInteger)num;
- -(void)pushIn;
- -(void)pullOut;
- -(void)printNumber;
- @end
- @implementationBox
- -(id)init{
- self=[superinit];
- if(self){
- [selfsetNumber:0];
- }
- returnself;
- }
- -(void)setNumber:(NSInteger)num{
- number=num;
- if(number>10){
- NSException*e=[SomeOverException
- exceptionWithName:@"BoxOverflowException"
- reason:@"Thelevelisabove100"
- userInfo:nil];
- @throwe;
- }elseif(number>=6){
- //throwwarning
- NSException*e=[SomethingException
- exceptionWithName:@"BoxWarningException"
- reason:@"Thelevelisaboveorat60"
- userInfo:nil];
- @throwe;
- }elseif(number<0){
- //throwexception
- NSException*e=[NSException
- exceptionWithName:@"BoxUnderflowException"
- reason:@"Thelevelisbelow0"
- userInfo:nil];
- @throwe;
- }
- }
- -(void)pushIn{
- [selfsetNumber:number+1];
- }
- -(void)pullOut{
- [selfsetNumber:number-1];
- }
- -(void)printNumber{
- NSLog(@"Boxnumberis:%d",number);
- }
- @end
这里写 [SomeOverExceptionexceptionWithName:@"BoxOverflowException" reason:@"The level is above 100"异常的名称和理由,在捕获时可以获取。
3、使用Box,在适当添加下捕获Box类的异常
3.1、在没超过6时,没有异常
- -(void)viewDidLoad
- {
- [superviewDidLoad];
- NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
- Box*box=[[Boxalloc]init];
- for(inti=0;i<5;i++){
- [boxpushIn];
- [boxprintNumber];
- }
- }
- for(inti=0;i<11;i++){
- [boxpushIn];
- [boxprintNumber];
- }
- 2012-07-0409:12:05.889ObjectiveCTest[648:f803]Boxnumberis:1
- 2012-07-0409:12:05.890ObjectiveCTest[648:f803]Boxnumberis:2
- 2012-07-0409:12:05.890ObjectiveCTest[648:f803]Boxnumberis:3
- 2012-07-0409:12:05.890ObjectiveCTest[648:f803]Boxnumberis:4
- 2012-07-0409:12:05.891ObjectiveCTest[648:f803]Boxnumberis:5
- 2012-07-0409:12:05.891ObjectiveCTest[648:f803]***Terminatingappduetouncaughtexception'BoxWarningException',reason:'Thenumberisaboveorat60'
3.3、加上异常处理
- for(inti=0;i<11;i++){
- @try{
- [boxpushIn];
- }
- @catch(SomethingException*exception){
- NSLog(@"%@%@",[exceptionname],[exceptionreason]);
- }
- @catch(SomeOverException*exception){
- NSLog(@"%@",[exceptionname]);
- }
- @finally{
- [boxprintNumber];
- }
- }
- 2012-07-0409:14:35.165ObjectiveCTest[688:f803]Boxnumberis:1
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]Boxnumberis:2
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]Boxnumberis:3
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]Boxnumberis:4
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]Boxnumberis:5
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]Boxnumberis:6
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]Boxnumberis:7
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]Boxnumberis:8
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.169ObjectiveCTest[688:f803]Boxnumberis:9
- 2012-07-0409:14:35.169ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.169ObjectiveCTest[688:f803]Boxnumberis:10
- 2012-07-0409:14:35.169ObjectiveCTest[688:f803]BoxOverflowException
- 2012-07-0409:14:35.225ObjectiveCTest[688:f803]Boxnumberis:11
3.4 、小于0时的异常
在Box类的setNumber里,当number小于0时,我们抛出普通异常。
- @try{
- [boxsetNumber:-10];
- }
- @catch(NSException*exception){
- NSLog(@"%@",[exceptionname]);
- }
- @finally{
- [boxprintNumber];
- }
- 2012-07-0409:17:42.405ObjectiveCTest[753:f803]BoxUnderflowException
- 2012-07-0409:17:42.406ObjectiveCTest[753:f803]Boxnumberis:-10
著作权声明:本文由http://blog.youkuaiyun.com/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。
异常处理捕获的语法:
- @try{
- <#statements#>
- }
- @catch(NSException*exception){
- <#handler#>
- }
- @finally{
- <#statements#>
- }
我们自定义两个异常类,看看异常异常处理的使用。
1、新建SomethingException,SomeOverException这两个类,都继承与NSException类。
SomethingException.h
- #import<Foundation/Foundation.h>
- @interfaceSomethingException:NSException
- @end
- #import"SomethingException.h"
- @implementationSomethingException
- @end
- #import<Foundation/Foundation.h>
- @interfaceSomeOverException:NSException
- @end
- #import<Foundation/Foundation.h>
- @interfaceBox:NSObject
- {
- NSIntegernumber;
- }
- -(void)setNumber:(NSInteger)num;
- -(void)pushIn;
- -(void)pullOut;
- -(void)printNumber;
- @end
- @implementationBox
- -(id)init{
- self=[superinit];
- if(self){
- [selfsetNumber:0];
- }
- returnself;
- }
- -(void)setNumber:(NSInteger)num{
- number=num;
- if(number>10){
- NSException*e=[SomeOverException
- exceptionWithName:@"BoxOverflowException"
- reason:@"Thelevelisabove100"
- userInfo:nil];
- @throwe;
- }elseif(number>=6){
- //throwwarning
- NSException*e=[SomethingException
- exceptionWithName:@"BoxWarningException"
- reason:@"Thelevelisaboveorat60"
- userInfo:nil];
- @throwe;
- }elseif(number<0){
- //throwexception
- NSException*e=[NSException
- exceptionWithName:@"BoxUnderflowException"
- reason:@"Thelevelisbelow0"
- userInfo:nil];
- @throwe;
- }
- }
- -(void)pushIn{
- [selfsetNumber:number+1];
- }
- -(void)pullOut{
- [selfsetNumber:number-1];
- }
- -(void)printNumber{
- NSLog(@"Boxnumberis:%d",number);
- }
- @end
这里写 [SomeOverExceptionexceptionWithName:@"BoxOverflowException" reason:@"The level is above 100"异常的名称和理由,在捕获时可以获取。
3、使用Box,在适当添加下捕获Box类的异常
3.1、在没超过6时,没有异常
- -(void)viewDidLoad
- {
- [superviewDidLoad];
- NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
- Box*box=[[Boxalloc]init];
- for(inti=0;i<5;i++){
- [boxpushIn];
- [boxprintNumber];
- }
- }
- for(inti=0;i<11;i++){
- [boxpushIn];
- [boxprintNumber];
- }
- 2012-07-0409:12:05.889ObjectiveCTest[648:f803]Boxnumberis:1
- 2012-07-0409:12:05.890ObjectiveCTest[648:f803]Boxnumberis:2
- 2012-07-0409:12:05.890ObjectiveCTest[648:f803]Boxnumberis:3
- 2012-07-0409:12:05.890ObjectiveCTest[648:f803]Boxnumberis:4
- 2012-07-0409:12:05.891ObjectiveCTest[648:f803]Boxnumberis:5
- 2012-07-0409:12:05.891ObjectiveCTest[648:f803]***Terminatingappduetouncaughtexception'BoxWarningException',reason:'Thenumberisaboveorat60'
3.3、加上异常处理
- for(inti=0;i<11;i++){
- @try{
- [boxpushIn];
- }
- @catch(SomethingException*exception){
- NSLog(@"%@%@",[exceptionname],[exceptionreason]);
- }
- @catch(SomeOverException*exception){
- NSLog(@"%@",[exceptionname]);
- }
- @finally{
- [boxprintNumber];
- }
- }
- 2012-07-0409:14:35.165ObjectiveCTest[688:f803]Boxnumberis:1
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]Boxnumberis:2
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]Boxnumberis:3
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]Boxnumberis:4
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]Boxnumberis:5
- 2012-07-0409:14:35.167ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]Boxnumberis:6
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]Boxnumberis:7
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]Boxnumberis:8
- 2012-07-0409:14:35.168ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.169ObjectiveCTest[688:f803]Boxnumberis:9
- 2012-07-0409:14:35.169ObjectiveCTest[688:f803]BoxWarningExceptionThenumberisaboveorat60
- 2012-07-0409:14:35.169ObjectiveCTest[688:f803]Boxnumberis:10
- 2012-07-0409:14:35.169ObjectiveCTest[688:f803]BoxOverflowException
- 2012-07-0409:14:35.225ObjectiveCTest[688:f803]Boxnumberis:11
3.4 、小于0时的异常
在Box类的setNumber里,当number小于0时,我们抛出普通异常。
- @try{
- [boxsetNumber:-10];
- }
- @catch(NSException*exception){
- NSLog(@"%@",[exceptionname]);
- }
- @finally{
- [boxprintNumber];
- }
- 2012-07-0409:17:42.405ObjectiveCTest[753:f803]BoxUnderflowException
- 2012-07-0409:17:42.406ObjectiveCTest[753:f803]Boxnumberis:-10
著作权声明:本文由http://blog.youkuaiyun.com/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!