@interface NSException : NSObject <NSCopying, NSCoding> {
@private
NSString *name;
NSString *reason;
NSDictionary *userInfo;
id reserved;
}
@try @catch是Objective-C 异常捕捉机制
@try存放可能出现异常的代码 - 发现异常@catch异常处理逻辑 - 捕捉异常 && 处理异常@finally回收资源 – 执行收尾
【使用方法】
NSException *exc = [[NSException alloc]initWithName:@"had error" reason:@"speak english" userInfo:nil];
@try {
if (![@"english" isEqualToString:@"chinese"]) {
@throw exc;
}
}
@catch ( NSException *exception ) {
NSLog(@"exception.name = %@" , exception.name);
NSLog(@"exception.reason = %@" , exception.reason);
}
@finally {
NSLog(@"@finally");
}
NSString *message = @"this is a error exception .";
[NSException raise:@"WKErrorException" format:message,nil];
推荐阅读:
http://www.jianshu.com/p/05aad21e319e
本文介绍Objective-C中的异常处理机制,包括@try、@catch和@finally的使用方法,并通过示例代码展示了如何创建和抛出异常。
2006

被折叠的 条评论
为什么被折叠?



