Objective-C - 异常处理(NSException)

苹果关于异常的详细文档:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Exceptions/Exceptions.html#//apple_ref/doc/uid/10000012i

 

 

关于自定义异常或者扩展:

Objective-C中处理异常是依赖于NSException实现的,它是异常处理的基类,它是一个实体类,而并非一个抽象类,所以你可以直接使用它或者继承它扩展使用:

1.直接使用,分两种,抛出默认的异常,和自定义自己的新的种类的异常:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
        NSException* ex = [[NSException alloc]initWithName:@"MyException"  
                                                    reason:@"b==0"   
                                                  userInfo:nil];  
        
        @try   
        {  
            int b = 0;   
            switch (b)    
            {  
                case 0:  
                    @throw(ex);//b=0,则抛出异常;  
                    break;  
                default:  
                    break;  
            }  
        }  
        @catch (NSException *exception)//捕获抛出的异常   
        {  
            NSLog(@"exception.name= %@" ,exception.name);
            NSLog(@"exception.reason= %@" ,exception.reason);
            NSLog(@"b==0 Exception!");  
        }  
        @finally   
        {  
            NSLog(@"finally!");  
        }  
        [ex release];  
        
    }
    return 0;
}

ps:

Initializes and returns a newly allocated exception object.

- (id)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.

Discussion

This is the designated initializer.

Availability
  • Available in iOS 2.0 and later.


2.扩展使用,这个推荐你需要自定义一些功能的时候使用,比如,当捕获到指定的异常的时候弹出警告框之类的:
@interface MyException : NSException    
-(void)popAlert  
@end

@implementation MyException   
  
- (void)popAlert  
{  
 //弹出报告异常原因的警告框 reason  
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Tips"   
                                                    message:self.reason   
                                                   delegate:nil   
                                          cancelButtonTitle:nil   
                                          otherButtonTitles:nil];  
      
    [alert show];  
    [alert release];  
}  
 @end 
 

 使用:

- (IBAction)btnClicked_Exception:(id)sender   
{  
    MyException* ex = [[MyException alloc]initWithName:@"MyException"  
                                                reason:@"除数为0了!"   
                                              userInfo:nil];  
      
     
    @try   
    {  
        int b = 0;   
        switch (b)    
        {  
            case 0:  
                @throw(ex);//b=0,则抛出异常;  
                break;  
            default:  
                break;  
        }  
    }  
  
  
    @catch (MyException *exception)//捕获抛出的异常   
    {  
  
      [exception popAlert];  
  
      NSLog(@"b==0 Exception!");   
    }  
   
    @finally   
    {   
      NSLog(@"finally!");   
    }   
  
    [ex release];  
}  
 这个时候,捕获到异常,它就会弹出警告框了。当然,你还可以在MyException里面加一些指定的异常的通用处理方法。

只要你愿意,你就可以随意的定制它!

 

 

 

 

 

 

 

 

你可能感兴趣的:(Objective-C)