I'm studying for my test in Object Oriented Programming and I was wondering if there is any case what so ever that considering the following code:
try {
do something
} catch (someException e) {
} finally {
do something
}
the finally
block will not execute?
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. If we call the System.
Yes, finally will be called after the execution of the try or catch code blocks. The only times finally won't be called are: If you invoke System.
finally will be executed even if there is return in try and catch block. If return is replaced by System. exit(0) in try and catch block in above code and an exception occurs before it,for any reason.
Yes, it is not mandatory to use catch block with finally.
Yes. If you crash the Java VM or otherwise muck things up via native code, cause the program to terminate, or loop/wait infinitely inside the try
block.
Those are the only three cases which will avoid executing the finally
block.
If you call System.exit(0)
in the try
. Or make something that makes the JVM quit or hang (like a deadlock). Otherwise - no.
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