Consider the following code:
@try {
if (something.notvalid)
{
return;
}
// do something else
} @catch (NSException *ex) {
// handle exception
} @finally {
NSLog(@"finally!");
}
If something
is not valid and I return from within the try, does the code in @finally
execute or not? I believe that it should but others I've spoken to don't think so and I'm unable to test this at the moment.
Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java.
Returning from inside a finally block will cause exceptions to be lost. A return statement inside a finally block will cause any exception that might be thrown in the try block to be discarded.
Yes, we can write a return statement of the method in catch and finally block. There is a situation where a method will have a return type and we can return some value at any part of the method based on the conditions.
A finally block always executes, regardless of whether an exception is thrown.
@finally code always executes according to here and here.
A @finally block contains code that must be executed whether an exception is thrown or not.
Yes. Oddly enough, it does. I'm not sure why, but I just built a test and tried a number of configurations and every time it did.
Here were the configs:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With