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