Objective-C语法之异常处理

网友投稿 272 2022-08-24

Objective-C语法之异常处理

Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。

异常处理捕获的语法:

[cpp]  ​​view plain​​​ ​​​copy​​

@try {<#statements#>}catch (NSException *exception) {<#handler#>}@finally {<#statements#>}

我们自定义两个异常类,看看异常异常处理的使用。

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

SomethingException.h

[cpp]  ​​view plain​​​ ​​​copy​​

#import @interface SomethingException : NSException@end

[cpp]  ​​view plain​​​ ​​​copy​​

#import "SomethingException.h"@implementation SomethingException@end

[cpp]  ​​view plain​​​ ​​​copy​​

#import @interface SomeOverException : NSException@end

[cpp]  ​​view plain​​​ ​​​copy​​

#import "SomeOverException.h"@implementation SomeOverException@end

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

[cpp]  ​​view plain​​​ ​​​copy​​

#import @interface Box : NSObject{NSInteger number;}-(void) setNumber: (NSInteger) num;-(void) pushIn;-(void) pullOut;-(void) printNumber;@end

[cpp]  ​​view plain​​​ ​​​copy​​

@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"BoxOverflowException""The level is above 100"userInfo: nil];throw e;else if ( number >= 6 ) {// throw warningNSException *e = [SomethingException"BoxWarningException""The level is above or at 60"userInfo: nil];throw e;else if ( number < 0 ) {// throw exceptionNSException *e = [NSException"BoxUnderflowException""The level is below 0"userInfo: nil];throw e;}}-(void) pushIn {[self setNumber: number + 1];}-(void) pullOut {[self setNumber: number - 1];}-(void) printNumber {"Box number is: %d", number);}@end

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

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

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

[cpp]  ​​view plain​​​ ​​​copy​​

- (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,产生异常

[cpp]  ​​view plain​​​ ​​​copy​​

for (int i = 0; i < 11; i++) {[box pushIn];[box printNumber];}

[cpp]  ​​view plain​​​ ​​​copy​​

2012-07-04 09:12:05.889 ObjectiveCTest[648:f803] Box number is: 12012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 22012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 32012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 42012-07-04 09:12:05.891 ObjectiveCTest[648:f803] Box number is: 52012-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​​

for (int i = 0; i < 11; i++) {try {[box pushIn];}catch (SomethingException *exception) {"%@ %@", [exception name], [exception reason]);}catch (SomeOverException *exception) {"%@", [exception name]);}@finally {[box printNumber];}}

运行,程序没有崩溃,打印结果:

[cpp]  ​​view plain​​​ ​​​copy​​

2012-07-04 09:14:35.165 ObjectiveCTest[688:f803] Box number is: 12012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 22012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 32012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 42012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 52012-07-04 09:14:35.167 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 602012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 62012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 602012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 72012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 602012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 82012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 602012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 92012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 602012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 102012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxOverflowException2012-07-04 09:14:35.225 ObjectiveCTest[688:f803] Box number is: 11

3.4 、小于0时的异常

在Box类的setNumber里,当number小于0时,我们抛出普通异常。

[cpp]  ​​view plain​​​ ​​​copy​​

@try {[box setNumber:-10];}catch (NSException *exception) {"%@",[exception name]);}@finally {[box printNumber];}

[cpp]  ​​view plain​​​ ​​​copy​​

2012-07-04 09:17:42.405 ObjectiveCTest[753:f803] BoxUnderflowException2012-07-04 09:17:42.406 ObjectiveCTest[753:f803] Box number is: -10

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:苹果营销课:如何把偷工减料说得清新脱俗!
下一篇:Core Data浅谈系列之七 : 使用NSFetchedResultsController
相关文章

 发表评论

暂时没有评论,来抢沙发吧~