Objective-C语言使用try catch块进行错误和异常处理
示例
异常表示程序员级别的错误,例如尝试访问不存在的数组元素。
错误是用户级别的问题,例如尝试加载不存在的文件。因为在程序正常执行期间会出现错误。
示例
NSArray *inventory = @[@"Sam",
@"John",
@"Sanju"];
int selectedIndex = 3;
@try {
NSString * name = inventory[selectedIndex];
NSLog(@"The selected Name is: %@", name);
} @catch(NSException *theException) {
NSLog(@"An exception occurred: %@", theException.name);
NSLog(@"Here are some details: %@", theException.reason);
} @finally {
NSLog(@"Executing finally block");
}输出:
发生异常:NSRangeException
以下是一些详细信息:***-[__NSArrayIobjectAtIndex:]:索引3超出范围[0..2]
执行最终阻止
热门推荐
2 训练狗狗成为
3 训练狗狗几岁
4 反应训练狗狗
5 听力训练狗狗
7 狗狗尿训练
8 拳击训练狗狗
10 狗狗训练卧室
11 一个月狗狗怎么训练
12 几个月训练狗狗大小便
13 牧羊犬训练狗狗
14 狗狗训练做
15 狗狗训练它
16 可以训练狗狗
17 训练狗狗给球
18 训练狗狗的技巧