OC异常处理

简介: Person.h: #import #import @interface Cup : NSObject{    int level;   //成员变量-睡的深度值}-(int)level;   //获取水的深度-(void)setLevel:(i...
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);

}

@end

其他还有两个空类:CupWarningException;CupoverflowException
main:
 
 

#import <Foundation/Foundation.h>

#import "Person.h"

#import "CupOverflowException.h"

#import "CupWarningException.h"

#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.054 testOC[4063:303] CupWarningException The level is above or at 50

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

相关文章
|
6月前
|
缓存 Java iOS开发
OC中动态方法解析
OC中动态方法解析
26 1
|
iOS开发
OC中的内省方法(Introspection)
OC中的内省方法(Introspection)
126 0