object-c的异常处理机制
一直听说iOS有异常处理机制,却从来没有关系过,今天小生就来关心下iOS的异常处理机制吧。
Cup *cup = [[Cup alloc] init];
@try { [cup fill];
} @catch (NSException *exception) {
NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
} @finally {
[cup release];
}
抛出异常
为了掷出一个异常,我们必须实例化一个对象,当然这个对象要包含相关的信息,比如异常的名字和为什么要掷出他。
NSException *exception = [NSException exceptionWithName:@"HotTeaException" reason:@"The tea is too hot" userInfo:nil];
@throw exception;
(好吧 我承认国外的程序员挺有爱的。)
和@catch()块相反,你可以使用@throw再次掷出一个被抓到的异常,不用加参数哦亲。这个能使你的代码更可读。(我怎么没看出来)
Listing 9-1An exception handler @try {
...
} @catch (CustomException *ce) { //1
...
} @catch (NSException *ne) { //2
// Perform processing necessary at this level. ...
// Rethrow the exception so that it's handled at a higher level. @throw;
} @catch (id ue) { //3
...
} @finally { //4
// Perform processing necessary whether an exception occurred or not. ...
}
相关文章
- 这几种Java异常处理方法,你会吗?
- java quartz 失败重试_Quartz 2 定时任务(三):异常与中断处理
- 深入对比数据科学工具箱:Python和R的异常处理机制
- PYTHON 异常处理 二 TRY 模块
- c++ 异常处理(3)
- [Effective Java]第九章 异常
- Java 中的异常处理机制的简单原理和应用?
- [C++]Linux之C编程异常[true未定义解决方案]
- SpringBoot 全局异常处理进阶:使用 @ControllerAdvice 对不同的 Controller 分别捕获异常并处理
- JAVA的异常处理
- linux kernel的异常量表介绍(irq,fiq,swi,svc...)
- MultiTigger 绑定异常处理
- Java异常处理
- ios之网络异常与正常视图的切换
- 第二十二章:Java编程之异常处理机制
- 已解决AttributeError: ‘str‘ object has no attribute ‘decode‘解决方法异常的正确解决方法,亲测有效!!!
- 如何解决EnterLib异常处理框架最大的局限——基于异常"类型"的异常处理策略
- Communications link failure due to underlying exception异常处理(转)
- Mysql连接数据库异常汇总【必收藏】
- PHP 错误 与 异常处理(异常不会自动捕获抛出)
- 编程语言之 Python入门到精通.(基本知识:函数,Bug的由来及分类,异常处理机制等.)