objective-c 异常处理

系统提供的异常处理:NSException

继承于:NSObject

确认的协议: NSCopying,NSCoding,NSObject

声明:

+ (NSException *)exceptionWithName:(NSString *)name
                            reason:(NSString *)reason
                          userInfo:(NSDictionary *)userInfo

Parameters

name

The name of the exception.

reason

A human-readable message string summarizing the reason for the exception.

userInfo

A dictionary containing user-defined information relating to the exception

Return Value

The created NSException object or nil if the object couldn't be created.



- initWithName:reason:userInfo:  Designated Initializer

Initializes and returns a newly allocated exception object.

Declaration

OBJECTIVE-C

- (instancetype)initWithName:(NSString *)name
                      reason:(NSString *)reason
                    userInfo:(NSDictionary *)userInfo

Parameters

name

The name of the exception.

reason

A human-readable message string summarizing the reason for the exception.

userInfo

A dictionary containing user-defined information relating to the exception

Return Value

The created NSException object or nil if the object couldn't be created.

oc异常的处理 使用的格式如下:

    @try {
        <#Code that can potentially throw an exception#>
    }
    @catch (NSException *exception) {
        <#Handle an exception thrown in the @try block#>
    }
    @finally {
        <#Code that gets executed whether or not an exception is thrown#>
    }
@try中执行你的要执行的程序段

@catch中捕获@try抛出的异常

@finally是必须执行的语句

以下的例子则使用了系统自带的异常类和自己写的异常处理示例


//  numberexception.h
//  test1
//
//  Created by ethyn on 15/6/6.
//  Copyright (c) 2015年 ethyn. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface numberexception : NSObject
{
    int number;
    NSString *_name;
    NSString *_reason;
}
@property (readwrite,nonatomic) NSString *name;
@property (readwrite,nonatomic) NSString *reason;

-(void)setNumber:(int)newNumber;
-(int)getNumber;
-(void)initnum;
-(int)divide:(int)X andY:(int) Y;
-(id)printerrorWithName:(NSString *)newname reason:(NSString *)newreason;

@end

//
//  numberexception.m
//  test1
//
//  Created by ethyn on 15/6/6.
//  Copyright (c) 2015年 ethyn. All rights reserved.
//

#import "numberexception.h"

@implementation numberexception
@synthesize name = _name;
@synthesize reason = _reason;



-(void)setNumber:(int)newNumber
{
    number = newNumber;

    if (number == 100) {
       // NSException *e = ns
    }
    
}
-(id)printerrorWithName:(NSString *)newname reason:(NSString *)newreason
{
    if (self !=nil) {
        _name = newname ;
        _reason = newreason;
    }
    return self;
    
}




-(int)divide:(int)X andY:(int) Y
{
    number = X;
    
    
    @try {
        if(Y== 0)
        {
            NSException *e = [[NSException alloc]initWithName:@"Error1" reason:@"param2 can not be 0" userInfo:nil];
            @throw e;
        }
        if (Y == 2) {
            NSException *e2 = [NSException exceptionWithName:@"Error2" reason:@"param2 is 2" userInfo:nil];
            @throw e2;
        }
        if (Y==3) {
            numberexception *p = [[numberexception alloc]printerrorWithName:@"Error3" reason:@"param2 is 3"];
            @throw p;
            
        }
        
        return X/Y;
    }
    @catch (NSException *exception) {
        NSLog(@"exception name is %@,reason is %@\r\n",[exception name],[exception reason]);
        return -1;
        
    }
    @catch(numberexception *p)
    {
        NSLog(@"error name is %@ ,reason is %@\r\n",[p name],[p reason]);
    }
    @finally {
        NSLog(@" fianally %d/%d is hehe\r\n ",X,Y);
        
    }
}
-(void)initnum
{
    number = 0;
    _name =@"";
    _reason =@"";
}


-(int)getNumber
{
    return number;
}



@end

//main.m
    numberexception *mynumber = [[numberexception alloc]init];
    [mynumber initnum];
    [mynumber setNumber:10];
    [mynumber divide:10 andY:0];
    [mynumber divide:15 andY:3];
    NSLog(@"20/4 == %d\r\n",[mynumber divide:20 andY:4]);
    NSLog(@"number is %d\r\n",[mynumber getNumber]);


程序运行的结果如下:

2015-06-07 15:01:53.693 test1[1778:50076] exception name is Error1,reason is param2 can not be 0

2015-06-07 15:01:53.694 test1[1778:50076]  fianally 10/0 is hehe

2015-06-07 15:01:53.694 test1[1778:50076] error name is Error3 ,reason is param2 is 3

2015-06-07 15:01:53.694 test1[1778:50076]  fianally 15/3 is hehe

2015-06-07 15:01:53.694 test1[1778:50076]  fianally 20/4 is hehe

2015-06-07 15:01:53.694 test1[1778:50076] 20/4 == 5

2015-06-07 15:01:53.694 test1[1778:50076] number is 20





猜你喜欢

转载自blog.csdn.net/humanspider1/article/details/46401063
今日推荐