OC异常处理
来源:互联网 发布:java script实例 编辑:程序博客网 时间:2024/06/16 12:03
#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
@interface Cup : NSObject
{
int level; //成员变量-睡的深度值
}
-(int)level; //获取水的深度
-(void)setLevel:(int)i; //设置水的深度值
-(void)fill; //水的深度值增加10
-(void)empty; //水的深度值减少10
-(void)print; //输出水的深度值
-(double)set:(double)a over:(double)b; //计算一个百分比
@end
其他还有两个空类:CupWarningException;CupoverflowException#import "Person.h"
#import "CupOverflowException.h"
#import "CupWarningException.h"
@implementation Cup:NSObject;
-(id)init //初始化函数
{
self = [super init];
if(self)
{
[self setLevel:0]; //初始化水的深度值为0
}
return self;
}
-(int)level //获取水的深度值
{
return level;
}
-(double)set:(double)_a over:(double)_b //计算一个百分比
{
return _a/_b;
}
-(void)setLevel:(int)i
{
level = i;
if(level>100)
{
NSException *e = [CupOverflowException exceptionWithName:@"CupOverflowException" reason:@"The level is above 100" userInfo:nil];
@throw e;
}
else if(level>=50)
{
NSException *e = [CupWarningException exceptionWithName:@"CupWarningException" reason:@"The level is above or at 50" userInfo:nil];
@throw e; //抛出警告异常
}
else if(level<0)
{
NSException *e = [NSException exceptionWithName:@"CupunderflowException" reason:@"The level is blow 0" userInfo:nil];
@throw e; //抛出异常
}
}
-(void)fill //设置水杯的水的深度值增加10
{
[self setLevel:level+10];
}
-(void)empty //设置水杯的水的深度减少10
{
[self setLevel:level-10];
}
-(void)print //输出水杯内水的深度值
{
NSLog(@"Cup level is:%d",level);
}
@end
结果:#import <Foundation/Foundation.h>
#import "Person.h"
#import "CupOverflowException.h"
#import "CupWarningException.h"
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>
int main(int argc, const char * argv[])
{
//创建一个自动释放池
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
//分配并初始化一个水杯对象
Cup *cup = [[Cup alloc]init];
int i;
for(i=0;i<4;i++)
{
[cup fill];
[cup print];
}
for (i=0;i<7;i++) {
@try {
[cup fill];
}
@catch (CupWarningException *e) {
NSLog(@"%@ %@",[e name],[e reason]); //输出警告异常信息
}
@catch (CupOverflowException *e) {
NSLog(@"%@ %@",[e name],[e reason]); //输出溢出异常信息
}
@finally {
[cup print];
}
}
@try {
[cup setLevel:-1];
}
@catch (NSException *e) {
NSLog(@"%@ %@",[e name],[e reason]); //输出深度小于0的异常信息
}
[pool release];
[cup release];
}
CupWarningException The level is above or at 50
2013-07-31 15:53:32.053 testOC[4063:303] Cup level is:50
2013-07-31 15:53:32.053 testOC[4063:303] CupWarningException The level is above or at 50
2013-07-31 15:53:32.053 testOC[4063:303] Cup level is:60
2013-07-31 15:53:32.054 testOC[4063:303] CupWarningException The level is above or at 50
2013-07-31 15:53:32.054 testOC[4063:303] Cup level is:70
2013-07-31 15:53:32.054 testOC[4063:303] CupWarningException The level is above or at 50
2013-07-31 15:53:32.055 testOC[4063:303] Cup level is:80
2013-07-31 15:53:32.055 testOC[4063:303] CupWarningException The level is above or at 50
2013-07-31 15:53:32.055 testOC[4063:303] Cup level is:90
2013-07-31 15:53:32.056 testOC[4063:303] CupWarningException The level is above or at 50
2013-07-31 15:53:32.056 testOC[4063:303] Cup level is:100
2013-07-31 15:53:32.056 testOC[4063:303] CupOverflowException The level is above 100
2013-07-31 15:53:32.089 testOC[4063:303] Cup level is:110
2013-07-31 15:53:32.090 testOC[4063:303] CupunderflowException The level is blow 0
- OC异常处理
- oc的异常处理
- OC 异常处理
- OC 异常处理
- OC异常处理大全
- OC使用@try处理异常
- OC视频笔记4.8(异常处理)
- OC 异常消息转发处理流程
- OC语法--nil、 Nil、 NULL 、NSNul用法及OC异常处理
- oc 异常报错
- OC的异常机制
- 3.7 iOS开发视频教程—OC中动态绑定和异常处理
- 【IOS 开发学习总结-OC-22】★objective-c——使用@try 处理异常
- 异常处理+异常+处理异常+自定义异常
- OC-文件处理
- OC 学习 字符串处理
- OC字符串处理
- OC学习-block处理
- 内核编译的步骤及遇到的问题
- Effective STL 读书总结
- 开始写博客
- 个人重构机房收费系统SqlHelper的使用
- mini2440 dm9000 网卡驱动详解 (一)
- OC异常处理
- CSS的特性以及在HTML中的三种用法
- 最短路入门练习 解题报告
- uva 784 Maze Exploration(DFS遍历图)
- 序列化与反序列化 代码总结
- 序列化后字符放到数组
- Linux 压解命令
- 关于vector类的补充和一些库工具
- HDU 2222 Keywords Search(AC自动机模板题)