Objective-C语法之异常处理

Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。
异常处理捕获的语法:
[cpp]  view plain copy
  1. @try {  
  2.       <#statements#>  
  3.   }  
  4.   @catch (NSException *exception) {  
  5.       <#handler#>  
  6.   }  
  7.   @finally {  
  8.       <#statements#>  
  9.   }  
 @catch{} 块 对异常的捕获应该先细后粗,即是说先捕获特定的异常,再使用一些泛些的异常类型。
我们自定义两个异常类,看看异常异常处理的使用。

1、新建SomethingException,SomeOverException这两个类,都继承与NSException类。

SomethingException.h
[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface SomethingException : NSException  
  4.   
  5. @end  
SomethingException.m
[cpp]  view plain copy
  1. #import "SomethingException.h"  
  2.   
  3. @implementation SomethingException  
  4.   
  5. @end  
SomeOverException.h
[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface SomeOverException : NSException  
  4.   
  5. @end  
SomeOverException.m
[cpp]  view plain copy
  1. #import "SomeOverException.h"  
  2.   
  3. @implementation SomeOverException  
  4.   
  5. @end  

2、新建Box类,在某些条件下产生异常。

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Box : NSObject  
  4. {  
  5.     NSInteger number;  
  6. }  
  7. -(void) setNumber: (NSInteger) num;  
  8. -(void) pushIn;  
  9. -(void) pullOut;  
  10. -(void) printNumber;  
  11. @end  
 
[cpp]  view plain copy
  1. @implementation Box  
  2. -(id) init {  
  3.     self = [super init];  
  4.       
  5.     if ( self ) {  
  6.         [self setNumber: 0];  
  7.     }  
  8.       
  9.     return self;  
  10. }  
  11.   
  12. -(void) setNumber: (NSInteger) num {  
  13.     number = num;  
  14.       
  15.     if ( number > 10 ) {  
  16.         NSException *e = [SomeOverException  
  17.                           exceptionWithName: @"BoxOverflowException"  
  18.                           reason: @"The level is above 100"  
  19.                           userInfo: nil];  
  20.         @throw e;  
  21.     } else if ( number >= 6 ) {  
  22.         // throw warning  
  23.         NSException *e = [SomethingException  
  24.                           exceptionWithName: @"BoxWarningException"  
  25.                           reason: @"The level is above or at 60"  
  26.                           userInfo: nil];  
  27.         @throw e;  
  28.     } else if ( number < 0 ) {  
  29.         // throw exception  
  30.         NSException *e = [NSException  
  31.                           exceptionWithName: @"BoxUnderflowException"  
  32.                           reason: @"The level is below 0"  
  33.                           userInfo: nil];  
  34.         @throw e;  
  35.     }  
  36. }  
  37.   
  38. -(void) pushIn {  
  39.     [self setNumber: number + 1];  
  40. }  
  41.   
  42. -(void) pullOut {  
  43.     [self setNumber: number - 1];  
  44. }  
  45.   
  46. -(void) printNumber {  
  47.     NSLog(@"Box number is: %d", number);  
  48. }  
  49. @end  
这个类的作用是,初始化Box时,number数字是0,可以用pushIn 方法往Box里推入数字,每调用一次,number加1.当number数字大于等于6时产生SomethingException异常,告诉你数字达到或超过6了,超过10时产生SomeOverException异常,小于1时产生普通的NSException异常。

这里写 [SomeOverException  exceptionWithName:@"BoxOverflowException"  reason:@"The level is above 100"异常的名称和理由,在捕获时可以获取。

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

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

[cpp]  view plain copy
  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,产生异常

[cpp]  view plain copy
  1. for (int i = 0; i < 11; i++) {  
  2.             [box pushIn];  
  3.             [box printNumber];  
  4.     }  
[cpp]  view plain copy
  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、加上异常处理

[cpp]  view plain copy
  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.     }  
运行,程序没有崩溃,打印结果:
[cpp]  view plain copy
  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时,我们抛出普通异常。
[cpp]  view plain copy
  1. @try {  
  2.       [box setNumber:-10];  
  3.   }  
  4.   @catch (NSException *exception) {  
  5.       NSLog(@"%@",[exception name]);  
  6.   }  
  7.   @finally {  
  8.       [box printNumber];  
  9.   }  
打印结果:
[cpp]  view plain copy
  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  

著作权声明:本文由http://blog.csdn.net/totogo2010/原创

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 智慧社区背景与挑战 随着城市化的快速发展,社区面临健康、安全、邻里关系和服务质量等多方面的挑战。华为技术有限公司提出智慧社区解决方案,旨在通过先进的数字化技术应对这些问题,提升城市社区的生活质量。 2. 技术推动智慧社区发展 技术进步,特别是数字化、无线化、移动化和物联化,为城市社区的智慧化提供了可能。这些技术的应用不仅提高了社区的运行效率,也增强了居民的便利性和安全性。 3. 智慧社区的核心价值 智慧社区承载了智慧城市的核心价值,通过全面信息化处理,实现对城市各个方面的数字网络化管理、服务与决策功能,从而提升社会服务效率,整合社会服务资源。 4. 多层次、全方位的智慧社区服务 智慧社区通过构建和谐、温情、平安和健康四大社区模块,满足社区居民的多层次需求。这些服务模块包括社区医疗、安全监控、情感沟通和健康监测等。 5. 智慧社区技术框架 智慧社区技术框架强调统一平台的建设,设立数据中心,构建基础网络,并通过分层建设,实现平台能力及应用的可持续成长和扩展。 6. 感知统一平台与服务方案 感知统一平台是智慧社区的关键组成部分,通过统一的RFID身份识别和信息管理,实现社区服务的智能化和便捷化。同时,提供社区内外监控、紧急救助服务和便民服务等。 7. 健康社区的构建 健康社区模块专注于为居民提供健康管理服务,通过整合医疗资源和居民接入,实现远程医疗、慢性病管理和紧急救助等功能,推动医疗模式从治疗向预防转变。 8. 平安社区的安全保障 平安社区通过闭路电视监控、防盗报警和紧急求助等技术,保障社区居民的人身和财产安全,实现社区环境的实时监控和智能分析。 9. 温情社区的情感沟通 温情社区着重于建立社区居民间的情感联系,通过组织社区活动、一键呼叫服务和互帮互助平台,增强邻里间的交流和互助。 10. 和谐社区的资源整合 和谐社区作为社会资源的整合协调者,通过统一接入和身份识别,实现社区信息和服务的便捷获取,提升居民生活质量,促进社区和谐。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值