@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