您的位置:首页 > 移动开发 > Objective-C

Objective-C - 异常处理(NSException)

2016-06-15 10:12 363 查看
苹果关于异常的详细文档: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Exceptions/Exceptions.html#//apple_ref/doc/uid/10000012i  
 
 
关于自定义异常或者扩展:
Objective-C中处理异常是依赖于NSException实现的,它是异常处理的基类,它是一个实体类,而并非一个抽象类,所以你可以直接使用它或者继承它扩展使用:
1.直接使用,分两种,抛出默认的异常,和自定义自己的新的种类的异常:
 
C代码 
1 #import <Foundation/Foundation.h>  
2   
3 int main (int argc, const char * argv[])  
4 {  
5       
6     @autoreleasepool {  
7         NSException* ex = [[NSException alloc]initWithName:@"MyException"    
8                                                     reason:@"b==0"     
9                                                   userInfo:nil];    
10                
11              @try     
12              {    
13                  int b = 0;     
14                  switch (b)      
15                  {    
16                      case 0:    
17                          @throw(ex);//b=0,则抛出异常;    
18                          break;    
19                      default:    
20                          break;    
21                  }    
22              }    
23              @catch (NSException *exception)//捕获抛出的异常     
24              {    
25                  NSLog(@"exception.name= %@" ,exception.name);  
26                  NSLog(@"exception.reason= %@" ,exception.reason);  
27                  NSLog(@"b==0 Exception!");    
28              }    
29              @finally     
30              {    
31                  NSLog(@"finally!");    
32              }    
33              [ex release];    
34                
35          }  
36          return 0;  
37      }  
ps:
Initializesand returns a newly allocated exception object.
- (id)initWithName:(NSString *)name reason:(NSString *)reason userInfo:(NSDictionary *)userInfo
Parameters
name
Thename of the exception.
reason
Ahuman-readable message string summarizing the reason for the exception.
userInfo
Adictionary containing user-defined information relating to the exception
ReturnValue
Thecreated NSException object or nil if the object couldn't be created.
Discussion
Thisis the designated initializer.
Availability
•  Available in iOS 2.0 and later.
 
 
2.扩展使用,这个推荐你需要自定义一些功能的时候使用,比如,当捕获到指定的异常的时候弹出警告框之类的:
C代码 
1 @interface MyException : NSException      
2 -(void)popAlert    
3 @end  
 
Java代码 
1 @implementation MyException     
2     
3 - (void)popAlert    
4 {    
5  //弹出报告异常原因的警告框 reason    
6     UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Tips"     
7                                                     message:self.reason     
8                                                    delegate:nil     
9                                           cancelButtonTitle:nil     
10                                                otherButtonTitles:nil];    
11              
12          [alert show];    
13          [alert release];    
14      }    
15       @end   
 
 使用:
 
C代码 
1 - (IBAction)btnClicked_Exception:(id)sender     
2 {    
3     MyException* ex = [[MyException alloc]initWithName:@"MyException"    
4                                                 reason:@"除数为0了!"     
5                                               userInfo:nil];    
6         
7        
8     @try     
9     {    
10              int b = 0;     
11              switch (b)      
12              {    
13                  case 0:    
14                      @throw(ex);//b=0,则抛出异常;    
15                      break;    
16                  default:    
17                      break;    
18              }    
19          }    
20          
21          
22          @catch (MyException *exception)//捕获抛出的异常     
23          {    
24          
25            [exception popAlert];    
26          
27            NSLog(@"b==0 Exception!");     
28          }    
29           
30          @finally     
31          {     
32            NSLog(@"finally!");     
33          }     
34          
35          [ex release];    
36      }    
 这个时候,捕获到异常,它就会弹出警告框了。当然,你还可以在MyException里面加一些指定的异常的通用处理方法。
只要你愿意,你就可以随意的定制它!
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 异常