天天看點

OC異常處理

Person.h:

#import <Foundation/Foundation.h>

#import <Foundation/NSObject.h>

@interface Cup : NSObject

{

    int level;   //成員變量-睡的深度值

}

-(int)level;   //擷取水的深度

-(void)setLevel:(int)i;   //設定水的深度值

-(void)fill;   //水的深度值增加10

-(void)empty;   //水的深度值減少10

-(void)print;  //輸出水的深度值

-(double)set:(double)a over:(double)b;  //計算一個百分比

@end

Person.m:

#import "Person.h"

#import "CupOverflowException.h"

#import "CupWarningException.h"

@implementation Cup:NSObject;

-(id)init  //初始化函數

    self = [super init];

    if(self)

    {

        [self setLevel:0];   //初始化水的深度值為0

    }

    return self;

-(int)level    //擷取水的深度值

    return level;

-(double)set:(double)_a over:(double)_b  //計算一個百分比

    return _a/_b;

-(void)setLevel:(int)i

    level = i;

    if(level>100)

        NSException *e = [CupOverflowException exceptionWithName:@"CupOverflowException" reason:@"The level is above 100" userInfo:nil];

        @throw e;

    else if(level>=50)

        NSException *e = [CupWarningException exceptionWithName:@"CupWarningException" reason:@"The level is above or at 50" userInfo:nil];

        @throw e;  //抛出警告異常

    else if(level<0)

        NSException *e = [NSException exceptionWithName:@"CupunderflowException" reason:@"The level is blow 0" userInfo:nil];

        @throw e;  //抛出異常

-(void)fill   //設定水杯的水的深度值增加10

    [self setLevel:level+10];

-(void)empty  //設定水杯的水的深度減少10

    [self setLevel:level-10];

-(void)print  //輸出水杯内水的深度值

    NSLog(@"Cup level is:%d",level);

其他還有兩個空類:CupWarningException;CupoverflowException

main:

#import <Foundation/NSException.h>

#import <Foundation/NSString.h>

#import <Foundation/NSAutoreleasePool.h>

int main(int argc, const char * argv[])

    //建立一個自動釋放池

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

    //配置設定并初始化一個水杯對象

    Cup *cup = [[Cup alloc]init];

    int i;

    for(i=0;i<4;i++)

        [cup fill];

        [cup print];

    for (i=0;i<7;i++) {

        @try {

            [cup fill];

        }

        @catch (CupWarningException *e) {

            NSLog(@"%@ %@",[e name],[e reason]);   //輸出警告異常資訊

        @catch (CupOverflowException *e) {

            NSLog(@"%@ %@",[e name],[e reason]);   //輸出溢出異常資訊

        @finally {

            [cup print];

    @try {

        [cup setLevel:-1];

    @catch (NSException *e) {

        NSLog(@"%@ %@",[e name],[e reason]);   //輸出深度小于0的異常資訊

    [pool release];

    [cup release];

結果:

CupWarningException The level is above or at 50

2013-07-31 15:53:32.053 testOC[4063:303] Cup level is:50

2013-07-31 15:53:32.053 testOC[4063:303] CupWarningException The level is above or at 50

2013-07-31 15:53:32.053 testOC[4063:303] Cup level is:60

2013-07-31 15:53:32.054 testOC[4063:303] CupWarningException The level is above or at 50

2013-07-31 15:53:32.054 testOC[4063:303] Cup level is:70

2013-07-31 15:53:32.055 testOC[4063:303] Cup level is:80

2013-07-31 15:53:32.055 testOC[4063:303] CupWarningException The level is above or at 50

2013-07-31 15:53:32.055 testOC[4063:303] Cup level is:90

2013-07-31 15:53:32.056 testOC[4063:303] CupWarningException The level is above or at 50

2013-07-31 15:53:32.056 testOC[4063:303] Cup level is:100

2013-07-31 15:53:32.056 testOC[4063:303] CupOverflowException The level is above 100

2013-07-31 15:53:32.089 testOC[4063:303] Cup level is:110

2013-07-31 15:53:32.090 testOC[4063:303] CupunderflowException The level is blow 0

本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366502,如需轉載請自行聯系原作者

繼續閱讀