鉴于以下情况:
@try {
@try {
// raises an exception :)
[receiver raisingFirstException];
} @finally {
// raises another exception :)
[otherReceiver raisingFinalException];
}
} @catch (id e) {
printf("exception: %s\n", [[e stringValue] cString]);
}
有没有办法要么获得第一个例外
@finally
阻止或在@catch
块中获取两个例外?
我有代码,其中@finally
块执行一些可能引发的检查
异常,但我不想松开原始异常(根本原因)。
如果没有原始异常,但检查失败,我想要 他们抛出的例外。
答案 0 :(得分:2)
执行此操作的最佳方法是将异常分配给可从块的其余部分访问的变量。
NSException *ex;
@try {
@try {
[someObject methodWhichCouldThrowException];
} @catch (NSException *e) {
ex = e;
} @finally {
[anotherObject methodWhichCouldThrowADifferentException];
}
} @catch (NSException *e) {
// From here you can access both the exception thrown by 'someObject'
// as well as the exception thrown by 'anotherObject'.
}