Objective-C语法之异常处理
来源:互联网 发布:华为matebook x知乎 编辑:程序博客网 时间:2024/05/16 00:38
Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。
异常处理捕获的语法:
- @try {
- <#statements#>
- }
- @catch (NSException *exception) {
- <#handler#>
- }
- @finally {
- <#statements#>
- }
我们自定义两个异常类,看看异常异常处理的使用。
1、新建SomethingException,SomeOverException这两个类,都继承与NSException类。
SomethingException.h
- #import <Foundation/Foundation.h>
- @interface SomethingException : NSException
- @end
- #import "SomethingException.h"
- @implementation SomethingException
- @end
- #import <Foundation/Foundation.h>
- @interface SomeOverException : NSException
- @end
- #import "SomeOverException.h"
- @implementation SomeOverException
- @end
2、新建Box类,在某些条件下产生异常。
- #import <Foundation/Foundation.h>
- @interface Box : NSObject
- {
- NSInteger number;
- }
- -(void) setNumber: (NSInteger) num;
- -(void) pushIn;
- -(void) pullOut;
- -(void) printNumber;
- @end
- @implementation Box
- -(id) init {
- self = [super init];
- if ( self ) {
- [self setNumber: 0];
- }
- return self;
- }
- -(void) setNumber: (NSInteger) num {
- number = num;
- if ( number > 10 ) {
- NSException *e = [SomeOverException
- exceptionWithName: @"BoxOverflowException"
- reason: @"The level is above 100"
- userInfo: nil];
- @throw e;
- } else if ( number >= 6 ) {
- // throw warning
- NSException *e = [SomethingException
- exceptionWithName: @"BoxWarningException"
- reason: @"The level is above or at 60"
- userInfo: nil];
- @throw e;
- } else if ( number < 0 ) {
- // throw exception
- NSException *e = [NSException
- exceptionWithName: @"BoxUnderflowException"
- reason: @"The level is below 0"
- userInfo: nil];
- @throw e;
- }
- }
- -(void) pushIn {
- [self setNumber: number + 1];
- }
- -(void) pullOut {
- [self setNumber: number - 1];
- }
- -(void) printNumber {
- NSLog(@"Box number is: %d", number);
- }
- @end
这里写 [SomeOverException exceptionWithName:@"BoxOverflowException" reason:@"The level is above 100"异常的名称和理由,在捕获时可以获取。
3、使用Box,在适当添加下捕获Box类的异常
3.1、在没超过6时,没有异常
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- Box *box = [[Box alloc]init];
- for (int i = 0; i < 5; i++) {
- [box pushIn];
- [box printNumber];
- }
- }
Box number is: 1
Box number is: 2
Box number is: 3
Box number is: 4
Box number is: 5
3.2 超过6,产生异常
- for (int i = 0; i < 11; i++) {
- [box pushIn];
- [box printNumber];
- }
- 2012-07-04 09:12:05.889 ObjectiveCTest[648:f803] Box number is: 1
- 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 2
- 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 3
- 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 4
- 2012-07-04 09:12:05.891 ObjectiveCTest[648:f803] Box number is: 5
- 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、加上异常处理
- for (int i = 0; i < 11; i++) {
- @try {
- [box pushIn];
- }
- @catch (SomethingException *exception) {
- NSLog(@"%@ %@", [exception name], [exception reason]);
- }
- @catch (SomeOverException *exception) {
- NSLog(@"%@", [exception name]);
- }
- @finally {
- [box printNumber];
- }
- }
- 2012-07-04 09:14:35.165 ObjectiveCTest[688:f803] Box number is: 1
- 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 2
- 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 3
- 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 4
- 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 5
- 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
- 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 6
- 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
- 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 7
- 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
- 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 8
- 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
- 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 9
- 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
- 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 10
- 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxOverflowException
- 2012-07-04 09:14:35.225 ObjectiveCTest[688:f803] Box number is: 11
3.4 、小于0时的异常
在Box类的setNumber里,当number小于0时,我们抛出普通异常。
- @try {
- [box setNumber:-10];
- }
- @catch (NSException *exception) {
- NSLog(@"%@",[exception name]);
- }
- @finally {
- [box printNumber];
- }
- 2012-07-04 09:17:42.405 ObjectiveCTest[753:f803] BoxUnderflowException
- 2012-07-04 09:17:42.406 ObjectiveCTest[753:f803] Box number is: -10
著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
0 0
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- 5,Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- 6、Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective C 语法系列--异常处理
- objective-c语法学习:异常处理
- LHF Objective-C语法(5)异常处理
- Objective-c异常处理
- objective-c 异常处理
- objective-c中的异常处理
- Objective-C - 异常处理(Exception)
- Sicily 8543. Trees
- 黑马程序员---类加载
- Sicily 7996. 购买电脑
- VS2010下使用QT环境配置
- asp.net MVC 学习(第一章) 认识MVC 以及 MVC 编码流程
- Objective-C语法之异常处理
- Linux下用iptables上网
- Sicily 11598. XOR
- 看cuda初级教程视频笔记(周斌讲的)--CUDA、GPU编程模型
- squid配置
- jquery 收集的知识点
- Linux chmod命令修改文件与文件夹权限的命令附实例
- 信管14:对象的成员访问示例
- "cannot be resolved or is not a field"问题解决