Java および dobjective-C プログラムを使用して例外をテストします。
これらのテストでは、例外がキャッチされて再スローされたときに、最終的にブロックに到達する方法に違いがあることがわかります。
ここで私の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
そしてここで私の目標-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
最終ブロックのコードに到達していません!
この違いはなぜですか?
[編集] すみません、@try ... @try ... @try... は間違いでした。変更しましたが、問題は同じです。objective-c テストで finally ブロックに到達できません