I test exceptions using in java an dobjective-C programs.
In these tests, I see a difference in the way of finally block is reached when an exception is catched and rethrown.
Here my java test :
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");
}
which displays :
try: before exception sent
catch, rethrow
finally
And here my objective-c test :
@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");
}
which displays :
try: before exception sent
catch, rethrown
*** Terminating app
Code in finally block is not reached !
Why this difference ?
[EDIT] Sorry, @try … @try … @try… was a mistake.
I changed it, but the problem is the same, i can’t reach finally block in objective-c test
OK, i solved my problem.
In my objective-c test, application crashed, that’s why finally block was not reached.
If I add a try catch block in the main, now in my function, finally block is reached !
So, I confirm that finally block is still reached whatever an excpetion occurs (and is rethrown) or not).