一个异常的例子
来源:互联网 发布:编辑歌曲的软件 编辑:程序博客网 时间:2024/06/10 21:37
異常情況(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: 10Cup level is: 20Cup level is: 30Cup level is: 40CupWarningException: Cup level is: 50CupWarningException: Cup level is: 60CupWarningException: Cup level is: 70CupWarningException: Cup level is: 80CupWarningException: Cup level is: 90CupWarningException: Cup level is: 100CupOverflowException: Cup level is: 110CupUnderflowException: 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 *。
e name 中的name函数是NSException中提供的 cString是NSString提供的
- 一个异常的例子
- 一个关于异常的例子
- 一个异常抛出的例子
- 一个异常抛出的例子
- struts异常的处理的一个例子
- java自定义异常的一个例子
- 一个自定制异常类的例子
- java异常中一个有趣的例子
- java面向对象-异常处理的一个例子
- 一个例子演示了C++异常的推荐用法
- Java中关于try{}catch{}抛出异常的一个例子
- Java异常的例子
- 简单的异常例子
- 异常的例子
- 自定义异常的例子
- Java的异常处理机制的一个例子-读后定能对异常理解深刻
- 关于处理try-catch循环一次异常,次次异常的一个例子
- 一个例子明白抛出与捕获异常
- hdu1712 ACboy needs your help
- vs2012 快速注释/取消注释
- hdu4698 Counting
- Java多线程(七)、Volatile 关键字
- HDU 1599 find the mincost route
- 一个异常的例子
- 黑马程序员-Java面向对象-day07
- 微生物
- windows文件比较命令
- ubuntu下安装配置SSH
- 章小结
- 打印100以内的素数
- Window.open() 全攻略
- VC2010中编译Sqlite3为静态库并带加密功能的方法