Objective-c异常处理
2010-10-28 10:49
323 查看
o 异常情况(Exceptions) 注意:异常处理只有 Mac OS X 10.3 以上才支持。 基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。 CupWarningException.h #import <Foundation/NSException.h> @interface CupWarningException: NSException @end CupWarningException.m #import "CupWarningException.h" @implementation CupWarningException @end CupOverflowException.h #import <Foundation/NSException.h> @interface CupOverflowException: NSException @end CupOverflowException.m #import "CupOverflowException.h" @implementation CupOverflowException @end Cup.h #import <Foundation/NSObject.h> @interface Cup: NSObject { int level; } -(int) level; -(void) setLevel: (int) l; -(void) fill; -(void) empty; -(void) print; @end Cup.m #import "Cup.h" #import "CupOverflowException.h" #import "CupWarningException.h" #import <Foundation/NSException.h> #import <Foundation/NSString.h> @implementation Cup -(id) init { self = [super init]; if ( self ) { [self setLevel: 0]; } return self; } -(int) level { return level; } -(void) setLevel: (int) l { level = l; if ( level > 100 ) { // throw overflow NSException *e = [CupOverflowException exceptionWithName: @"CupOverflowException" reason: @"The level is above 100" userInfo: nil]; @throw e; } else if ( level >= 50 ) { // throw warning NSException *e = [CupWarningException exceptionWithName: @"CupWarningException" reason: @"The level is above or at 50" userInfo: nil]; @throw e; } else if ( level < 0 ) { // throw exception NSException *e = [NSException exceptionWithName: @"CupUnderflowException" reason: @"The level is below 0" userInfo: nil]; @throw e; } } -(void) fill { [self setLevel: level + 10]; } -(void) empty { [self setLevel: level - 10]; } -(void) print { printf( "Cup level is: %i/n", level ); } @end main.m #import "Cup.h" #import "CupOverflowException.h" #import "CupWarningException.h" #import <Foundation/NSString.h> #import <Foundation/NSException.h> #import <Foundation/NSAutoreleasePool.h> #import <stdio.h> int main( int argc, const char *argv[] ) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Cup *cup = [[Cup alloc] init]; int i; // this will work for ( i = 0; i < 4; i++ ) { [cup fill]; [cup print]; } // this will throw exceptions for ( i = 0; i < 7; i++ ) { @try { [cup fill]; } @catch ( CupWarningException *e ) { printf( "%s: ", [[e name] cString] ); } @catch ( CupOverflowException *e ) { printf( "%s: ", [[e name] cString] ); } @finally { [cup print]; } } // throw a generic exception @try { [cup setLevel: -1]; } @catch ( NSException *e ) { printf( "%s: %s/n", [[e name] cString], [[e reason] cString] ); } // free memory [cup release]; [pool release]; } output Cup level is: 10 Cup level is: 20 Cup level is: 30 Cup level is: 40 CupWarningException: Cup level is: 50 CupWarningException: Cup level is: 60 CupWarningException: Cup level is: 70 CupWarningException: Cup level is: 80 CupWarningException: Cup level is: 90 CupWarningException: Cup level is: 100 CupOverflowException: Cup level is: 110 CupUnderflowException: The level is below 0 NSAutoreleasePool 是一个内存管理类别。现在先别管它是干嘛的。 Exceptions(异常情况)的丢出不需要扩充(extend)NSException 对象,你可简单的用 id 来代表它: @catch ( id e ) { ... } 还有一个 finally 区块,它的行为就像 Java 的异常处理方式,finally 区块的内容保证会被呼叫。 Cup.m 里的 @"CupOverflowException" 是一个 NSString 常数物件。在 Objective-C 中,@ 符号通常用来代表这是语言的衍生部分。C 语言形式的字符串(C string)就像 C/C++ 一样是 "String constant" 的形式,型别为 char *。
相关文章推荐
- c/objective-c/win32/mfc/qt 异常处理与总结
- c/objective-c/win32/mfc/qt 异常处理与总结
- 从 C++ 到 Objective-C:异常处理和多线程
- Objective-C语法之异常处理
- 从C#到Objective-C,循序渐进学习苹果开发(4)--代码块(block)和错误异常处理的理解
- Objective-C:除数为0的情况下异常的处理(检测、抛出、捕捉、处理)
- Objective-C中使用@try处理异常
- Objective-C语法之异常处理
- objective-c异常处理机制详解
- Objective-C 编程语言(11)异常处理----使能异常处理、异常处理
- 5,Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- Objective-C语法之异常处理
- 【IOS 开发】Objective - C 面向对象高级特性 - 包装类 | 类处理 | 类别 | 扩展 | 协议 | 委托 | 异常处理 | 反射
- Objective-C 编程语言官网文档(十一)-异常的处理
- 【IOS 开发】Objective - C 面向对象高级特性 - 包装类 | 类处理 | 类别 | 扩展 | 协议 | 委托 | 异常处理 | 反射
- Objective-C 编程语言官网文档(十一)-异常的处理
- [Objective-C]异常处理
- Objective-C中使用@try处理异常,防止程序崩溃