objective-c 异常处理

本文详细介绍了Objective-C中的NSException异常处理机制,包括如何创建异常对象及使用try-catch-finally块捕获并处理异常。并通过实例展示了如何自定义异常类来处理特定错误情况,并提供了异常处理的实际应用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统提供的异常处理: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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值