我在 java 中使用 dobjective-C 程序测试异常。
在这些测试中,我发现当异常被捕获并重新抛出时,finally 块的方式有所不同。
这是我的java测试:
try {
Boolean bThrow = true;
System.out.println("try : before exception sent");
if (bThrow) {
throw new Exception();
}
System.out.println("try : after sent");
}
catch (Exception e) {
System.out.println("catch, rethrow");
throw e;
}
finally {
System.out.println("finally");
}
显示:
try: before exception sent
catch, rethrow
finally
这里是我的 Objective-C 测试:
@try {
NSException *myexc = [NSException exceptionWithName:@"exceptionTest" reason:@"exceptionTest" userInfo:nil];
BOOL bThrow = YES;
NSLog(@"try : before exception sent");
if (bThrow) {
@throw myexc;
}
NSLog(@"try : after sent");
}
@catch (Exception *exception) {
NSLog(@"catch, rethrow");
@throw exception;
}
@finally {
NSLog(@"finally");
}
显示:
try: before exception sent
catch, rethrown
*** Terminating app
未达到 finally 块中的代码!
为什么会有这种差异?
[编辑] 抱歉,@try ... @try ... @try ... 是一个错误。我改变了它,但问题是一样的,我无法在objective-c测试中达到finally块