Objective-C - 异常处理(NSException)

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

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 -voidpopAlert    

3 @end  

 

Java代码 

1 @implementation MyException     

2     

3 voidpopAlert    

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里面加一些指定的异常的通用处理方法。

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值