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

Objective-C语法之异常处理

[日期:2012-07-05] 来源:Linux社区  作者:totogo2010 [字体:大 中 小]
Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。
异常处理捕获的语法:
  1. @try {  
  2.       <#statements#>  
  3.   }  
  4.   @catch (NSException *exception) {  
  5.       <#handler#>  
  6.   }  
  7.   @finally {  
  8.       <#statements#>  
  9.   }  
@catch{} 块 对异常的捕获应该先细后粗,即是说先捕获特定的异常,再使用一些泛些的异常类型。
我们自定义两个异常类,看看异常异常处理的使用。

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

SomethingException.h
  1. #import <Foundation/Foundation.h>   
  2.   
  3. @interface SomethingException : NSException  
  4.   
  5. @end  
SomethingException.m
  1. #import "SomethingException.h"   
  2.   
  3. @implementation SomethingException  
  4.   
  5. @end  
SomeOverException.h
  1. #import <Foundation/Foundation.h>   
  2.   
  3. @interface SomeOverException : NSException  
  4.   
  5. @end  
SomeOverException.m
  1. #import "SomeOverException.h"   
  2.   
  3. @implementation SomeOverException  
  4.   
  5. @end  

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

  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  
 
  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"异常的名称和理由,在捕获时可以获取。

linux
相关资讯       Objective-C 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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