Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。
异常处理捕获的语法:
@try {
<#statements#>
}
@catch (NSException *exception) {
<#handler#>
}
@finally {
<#statements#>
}
@catch{} 块 对异常的捕获应该先细后粗,即是说先捕获特定的异常,再使用一些泛些的异常类型。
我们自定义两个异常类,看看异常异常处理的使用。
1、新建SomethingException,SomeOverException这两个类,都继承与NSException类。
SomethingException.h
#import <Foundation/Foundation.h>
@interface SomethingException : NSException
@end
SomethingException.m
#import "SomethingException.h"
@implementation SomethingException
@end
SomeOverException.h
#import <Foundation/Foundation.h>
@interface SomeOverException : NSException
@end
SomeOverException.m
#import "SomeOverException.h"
@implementation SomeOverException
@end
2、新建Box类,在某些条件下产生异常。
#import <Foundation/Foundation.h>
@interface Box : NSObject
{
NSInteger number;
}
-(void) setNumber: (NSInteger) num;
-(void) pushIn;
-(void) pullOut;
-(void) printNumber;
@end
@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
exceptionWithName: @"BoxOverflowException"
reason: @"The level is above 100"
userInfo: nil];
@throw e;
} else if ( number >= 6 ) {
// throw warning
NSException *e = [SomethingException
exceptionWithName: @"BoxWarningException"
reason: @"The level is above or at 60"
userInfo: nil];
@throw e;
} else if ( number < 0 ) {
// throw exception
NSException *e = [NSException
exceptionWithName: @"BoxUnderflowException"
reason: @"The level is below 0"
userInfo: nil];
@throw e;
}
}
-(void) pushIn {
[self setNumber: number + 1];
}
-(void) pullOut {
[self setNumber: number - 1];
}
-(void) printNumber {
NSLog(@"Box number is: %d", number);
}
@end
这个类的作用是,初始化Box时,number数字是0,可以用pushIn 方法往Box里推入数字,每调用一次,number加1.当number数字大于等于6时产生SomethingException异常,告诉你数字达到或超过6了,超过10时产生
SomeOverException异常,小于1时产生普通的NSException异常。
这里写 [SomeOverExceptionexceptionWithName:@"BoxOverflowException" reason:@"The level is above 100"异常的名称和理由,在捕获时可以获取。
3、使用Box,在适当添加下捕获Box类的异常
3.1、在没超过6时,没有异常
- (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,产生异常
for (int i = 0; i < 11; i++) {
[box pushIn];
[box printNumber];
}
2012-07-04 09:12:05.889 ObjectiveCTest[648:f803] Box number is: 1
2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 2
2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 3
2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 4
2012-07-04 09:12:05.891 ObjectiveCTest[648:f803] Box number is: 5
2012-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、加上异常处理
for (int i = 0; i < 11; i++) {
@try {
[box pushIn];
}
@catch (SomethingException *exception) {
NSLog(@"%@ %@", [exception name], [exception reason]);
}
@catch (SomeOverException *exception) {
NSLog(@"%@", [exception name]);
}
@finally {
[box printNumber];
}
}
运行,程序没有崩溃,打印结果:
2012-07-04 09:14:35.165 ObjectiveCTest[688:f803] Box number is: 1
2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 2
2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 3
2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 4
2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 5
2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 6
2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 7
2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 8
2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 9
2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 10
2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxOverflowException
2012-07-04 09:14:35.225 ObjectiveCTest[688:f803] Box number is: 11
超过10时,SomeOverException
异常抛出。
3.4 、小于0时的异常
在Box类的setNumber里,当number小于0时,我们抛出普通异常。
@try {
[box setNumber:-10];
}
@catch (NSException *exception) {
NSLog(@"%@",[exception name]);
}
@finally {
[box printNumber];
}
打印结果:
2012-07-04 09:17:42.405 ObjectiveCTest[753:f803] BoxUnderflowException
2012-07-04 09:17:42.406 ObjectiveCTest[753:f803] Box number is: -10
著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
分享到:
相关推荐
Objective-C基础教程:1天玩转Objective-C语法
启动Ultraedit,依次打开:高级(A) --> 配置(C) --> 编辑器显示 --> 语法着色。找到文档的完整目录名称所指示的目录, 然后把objective-c.uew文件放在对应的目录下即可。
这儿的运行时系统扮演的角色类似于Objective-C语言的操作系统,Objective-C基于该系统来工作。本文档将具体介绍NSObject类以及Objective-C程序是如何和运行时系统交互的。特别地,本文档还给出来怎样在运行时动态地加
Objective-C语法总结(第一版)
Objective-C 基础语法 Objective-C 基础语法
XCode、Objective-C、Cocoa
使UltraEdit支持Objective-C语法高亮
Objective-C语言的许多决策可以在编译和运行时执行。只要有可能,它是动态的。这意味着Objective-C语言不仅需要一个编译器,还需要一个运行时系统来执行编译的代码。Runtime系统是一种用于Objective-C语言的操作系统...
《objective-c程序设计》通过大量的实例系统地介绍了objective-c语言的基本概念、语法规则、框架、类库及开发环境。读者在阅读《objective-c程序设计》后,可以掌握objective-c语言的基本内容,并进行实际的iphone/...
这是一个基于Objective-C语言的基础案例集。旨在用于给初学者快速了解Objective-C语言的语法。.zip
通过学习Objective-C的基本语法、类和对象、控制流程和方法等内容,你将能够编写简单的Objective-C程序并逐渐掌握更复杂的概念。继续学习和实践,你将能够开发各种类型的应用程序,并为用户提供出色的体验。祝你在...
Objective-C 是苹果公司采用的主要编程语言之一,也是开发 macOS 和 iOS 应用程序的主要语言之一。Objective-C 是一种强大而灵活的编程语言,它结合了 C 语言的性能和灵活性以及面向对象编程的特性。本教程提供了 ...
iOS开发之Objective-C语法快速参考(PDF文档).
Objective-C的语法与Cocoa框架.pdf,挺不错的教程
《Objective-C 程序设计(第4版)》作者假设读者没有面向对象程序语言或者C语言(Objective-C基础)编程经验,因此,初学者和有经验的程序员都可以使用这本《Objective-C 程序设计(第4版)》学习Objective-C。...
用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计...
帮助C++开发者,快速学习Objective-C语言核心语法。 一个非常实用的帮助文档
Objective-C语法之走进iphone的开发世界那些事儿 简单的ios开发界面,欢迎大家下载阅读。
很好的语法讲座,叫你快速入门,如何了解objc的语法知识!