OC异常处理大全
2015-12-09 18:53
267 查看
以下文章由两篇转载的文章组成,第一篇文章似乎有些不妥的地方 ,在里面有用红字标出
文章1
Objective-C中处理异常需要用到NSException类,它是所有异常的基类。你可以直接使用NSException类来捕获异常,也可以继承一个新的类。
第一步
你需要先定义一个自己的异常类,在.h文件中加入如下代码:
[java] view
plaincopy
@interface MyException : NSException
@end
接着在.m文件中加入:
[java] view
plaincopy
@implementation MyException
@end
除了类的名字以外你什么都不用写,不需要添加成员变量和方法。
第二步
用上面定义的类创建一个异常对象:
[java] view
plaincopy
NSException *e = [MyException exceptionWithName:@"MyException" //给异常取一个名字
reason:@"the b==0" //异常被触发的原因
userInfo:nil];//这个写nil就可以
//这里似乎用下面的用法更好: 不然白定义自己的异常类了
MyException *mye=[[MyException alloc] initWithName :@"fatal error" reason : @"Disc IO error" :userInfo : nil];
exceptionWithName是NSException提供的方法,用来创建一个异常对象。
第三步
Objective-C的异常处理流程和大多数语言的差不多,使用@try @catch @finally来捕获并处理异常。
[java] view
plaincopy
@try {
//判断是否有BUG 并抛出异常
if(b==0){//除数为0
@throw e;
}
}
@catch (MyException *e) {
//在这里处理异常
}
@finally {
//异常发生或不发生 这里都会执行
}
@throw 可以用来抛出异常,如果抛出了异常但是没有去处理程序将崩溃。
文章2
虽然在实际编程中NSException运用不多,但也不妨碍了解一下Cocoa异常编程。
异常处理是管理非典型事件(例如未被识别的消息)的过程,此过程将会中断正常的程序执行。如果没有足够的错误处理,遇到非典型事件时,程序可能立刻抛出(或者引发)一种被称之为异常的东西,然后结束运行。
程序抛出异常的原因多种多样,可由硬件导致也可由软件引起。异常的例子很多,包括被零除、下溢和上异之类的数学错误,调用未定义的指令(例如,试图调用一个没有定义的方法 )以及试图越界访问群体中的元素 。
Cocoa异常由NSException对象作为载体,下面是NSException的声明:
A
a short string that is used to uniquely identify the exception. The name is required.
A
a longer string that contains a “human-readable” reason for the exception. The reason is required.
An optional dictionary (
used to supply application-specific data to the exception handler. For example, if the return value of a method causes an exception to be raised, you could pass the return value to the exception handler through
你可以在异常捕获后提取有用的信息,还可以适当的弹出一个异常警告框。
抛出的异常必须是NSException或者其子类,不能是其他类。
下面提供示例代码:
参考:
http://www.apple.com.cn/developer/iphone/library/documentation/General/Conceptual/DevPedia-CocoaCore/ExceptionHandling.html
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html#//apple_ref/doc/uid/20000059-BBCHGJIJ
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Exceptions/Tasks/RaisingExceptions.html#//apple_ref/doc/uid/20000058-BBCCFIBF
自己总结:
①、NSException类的方法 raise ,等同于 @throw
②、@throw 变量名 和 @throw (变量名) 都可以
③、NSException: raise : format : 这一个类方法就可以完成 生成异常对象 + 抛出异常对象的操作
文章1
Objective-C中处理异常需要用到NSException类,它是所有异常的基类。你可以直接使用NSException类来捕获异常,也可以继承一个新的类。
第一步
你需要先定义一个自己的异常类,在.h文件中加入如下代码:
[java] view
plaincopy
@interface MyException : NSException
@end
接着在.m文件中加入:
[java] view
plaincopy
@implementation MyException
@end
除了类的名字以外你什么都不用写,不需要添加成员变量和方法。
第二步
用上面定义的类创建一个异常对象:
[java] view
plaincopy
NSException *e = [MyException exceptionWithName:@"MyException" //给异常取一个名字
reason:@"the b==0" //异常被触发的原因
userInfo:nil];//这个写nil就可以
//这里似乎用下面的用法更好: 不然白定义自己的异常类了
MyException *mye=[[MyException alloc] initWithName :@"fatal error" reason : @"Disc IO error" :userInfo : nil];
exceptionWithName是NSException提供的方法,用来创建一个异常对象。
第三步
Objective-C的异常处理流程和大多数语言的差不多,使用@try @catch @finally来捕获并处理异常。
[java] view
plaincopy
@try {
//判断是否有BUG 并抛出异常
if(b==0){//除数为0
@throw e;
}
}
@catch (MyException *e) {
//在这里处理异常
}
@finally {
//异常发生或不发生 这里都会执行
}
@throw 可以用来抛出异常,如果抛出了异常但是没有去处理程序将崩溃。
文章2
虽然在实际编程中NSException运用不多,但也不妨碍了解一下Cocoa异常编程。
异常处理是管理非典型事件(例如未被识别的消息)的过程,此过程将会中断正常的程序执行。如果没有足够的错误处理,遇到非典型事件时,程序可能立刻抛出(或者引发)一种被称之为异常的东西,然后结束运行。
异常的类型
程序抛出异常的原因多种多样,可由硬件导致也可由软件引起。异常的例子很多,包括被零除、下溢和上异之类的数学错误,调用未定义的指令(例如,试图调用一个没有定义的方法 )以及试图越界访问群体中的元素 。Cocoa异常由NSException对象作为载体,下面是NSException的声明:
1 @interface NSException : NSObject <NSCopying, NSCoding> { 2 @private 3 NSString *name; 4 NSString *reason; 5 NSDictionary *userInfo; 6 id reserved; 7 }
A
name—
a short string that is used to uniquely identify the exception. The name is required.
A
reason—
a longer string that contains a “human-readable” reason for the exception. The reason is required.
An optional dictionary (
userInfo)
used to supply application-specific data to the exception handler. For example, if the return value of a method causes an exception to be raised, you could pass the return value to the exception handler through
userInfo.
你可以在异常捕获后提取有用的信息,还可以适当的弹出一个异常警告框。
抛出的异常必须是NSException或者其子类,不能是其他类。
下面提供示例代码:
1 NSException* ex = [[NSException alloc] initWithName:@"ExceptionName" // just for test 2 reason:@"XXX" 3 userInfo:nil]; 4 5 CustomNSException* ex = [[CustomNSException alloc] initWithName:@"CustomNSExceptionName" // just for test 6 reason:@"XXX" 7 userInfo:nil]; 8 9 @try { 10 bool error = YES; 11 if (error) { 12 @throw ex; 13 } 14 } 15 16 @catch ( CustomNSException *exception ) { 17 NSLog(@"CustomNSException.name = %@" , CustomNSException.name); 18 NSLog(@"CustomNSException.reason = %@" , CustomNSException.reason); 19 20 // 弹出警告框,提示异常信息 21 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:CustomNSException.name 22 message:CustomNSException.reason 23 delegate:nil 24 cancelButtonTitle:nil 25 otherButtonTitles:nil]; 26 27 [alert show]; 28 [alert release]; 29 } 30 31 @catch ( NSException *exception ) { 32 NSLog(@"exception.name = %@" , exception.name); 33 NSLog(@"exception.reason = %@" , exception.reason); 34 } 35 36 @finally { 37 NSLog(@"@finally"); 38 }
参考:
http://www.apple.com.cn/developer/iphone/library/documentation/General/Conceptual/DevPedia-CocoaCore/ExceptionHandling.html
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html#//apple_ref/doc/uid/20000059-BBCHGJIJ
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Exceptions/Tasks/RaisingExceptions.html#//apple_ref/doc/uid/20000058-BBCCFIBF
自己总结:
①、NSException类的方法 raise ,等同于 @throw
②、@throw 变量名 和 @throw (变量名) 都可以
③、NSException: raise : format : 这一个类方法就可以完成 生成异常对象 + 抛出异常对象的操作
相关文章推荐
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
- Linux服务之ansible自动化
- java异常—— finally 子句+带资源的 try语句
- POJ 1019 number sequece数学处理
- 快速排序算法(QuickSort)
- echarts之tooltip-showContent
- Python GUI--Tkinter实践
- 这样查看告警邮件要慢一点……
- 1012.数字分类
- Could not find Developer Disk Image
- npm内网源搭建
- mysql-cluster搭建
- 4-7-word2003-图文混排和综合排版
- USACO 1.1
- QT3661刷单平台官方推荐人七师菲儿
- HTML5动画/游戏神器---AlloyStick
- echarts之tooltip-trigger
- EL表达式
- php curl 并发
- Heritrix 初步优化(应用ELFHash优化线程数量)