你好,游客 登录 注册 搜索
背景:
阅读新闻

Objective-C语法之异常处理

[日期:2012-07-05] 来源:Linux社区  作者:totogo2010 [字体:大 中 小]

3、使用Box,在适当添加下捕获Box类的异常

3.1、在没超过6时,没有异常

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];      
  4.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  5.     Box *box = [[Box alloc]init];  
  6.     for (int i = 0; i < 5; i++) {  
  7.         [box pushIn];  
  8.         [box printNumber];  
  9.     }  
  10. }  
打印结果:

Box number is: 1

Box number is: 2

Box number is: 3

Box number is: 4

Box number is: 5

3.2 超过6,产生异常

  1. for (int i = 0; i < 11; i++) {  
  2.             [box pushIn];  
  3.             [box printNumber];  
  4.     }  
 
  1. 2012-07-04 09:12:05.889 ObjectiveCTest[648:f803] Box number is: 1  
  2. 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 2  
  3. 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 3  
  4. 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 4  
  5. 2012-07-04 09:12:05.891 ObjectiveCTest[648:f803] Box number is: 5  
  6. 2012-07-04 09:12:05.891 ObjectiveCTest[648:f803] *** Terminating app due to uncaught exception 'BoxWarningException', reason: 'The number is above or at 60'  
这是时,程序抛出异常崩溃了。那怎么使程序不崩溃呢,做异常处理。

3.3、加上异常处理

  1. for (int i = 0; i < 11; i++) {  
  2.         @try {  
  3.             [box pushIn];  
  4.         }  
  5.         @catch (SomethingException *exception) {  
  6.             NSLog(@"%@ %@", [exception name], [exception reason]);  
  7.         }  
  8.         @catch (SomeOverException *exception) {  
  9.             NSLog(@"%@", [exception name]);  
  10.         }  
  11.         @finally {  
  12.             [box printNumber];  
  13.         }  
  14.     }  
运行,程序没有崩溃,打印结果:
  1. 2012-07-04 09:14:35.165 ObjectiveCTest[688:f803] Box number is: 1  
  2. 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 2  
  3. 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 3  
  4. 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 4  
  5. 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 5  
  6. 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60  
  7. 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 6  
  8. 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60  
  9. 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 7  
  10. 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60  
  11. 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 8  
  12. 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60  
  13. 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 9  
  14. 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60  
  15. 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 10  
  16. 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxOverflowException  
  17. 2012-07-04 09:14:35.225 ObjectiveCTest[688:f803] Box number is: 11  
超过10时,SomeOverException异常抛出。

3.4 、小于0时的异常

在Box类的setNumber里,当number小于0时,我们抛出普通异常。
  1. @try {  
  2.       [box setNumber:-10];  
  3.   }  
  4.   @catch (NSException *exception) {  
  5.       NSLog(@"%@",[exception name]);  
  6.   }  
  7.   @finally {  
  8.       [box printNumber];  
  9.   }  
打印结果:
  1. 2012-07-04 09:17:42.405 ObjectiveCTest[753:f803] BoxUnderflowException  
  2. 2012-07-04 09:17:42.406 ObjectiveCTest[753:f803] Box number is: -10  
linux
相关资讯       Objective-C 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款