I\'m a Java rookie and I was wondering, if I have the following typical Java code
public class MyApp {
public static void main(String[] args) {
try {
Basically yes, except for the note listed here (emphasis mine):
If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
Yes, the JVM always executes it. Gaurranteed.
Of course ... if the JVM itself dies (eg: System.exit()), then it's not in a position to gaurrantee anything. But the JVM dying is not a within-java issue.
In a word, yes.
Code in the finally block in Java always executes unless:
(from: http://java.sun.com/docs/books/tutorial/essential/exceptions/finally.html)
So, unless you explicitly call System.exit(int), or kill the process or thread externally, you can rely on it.
Erm, yep :) Whether your code enters a catch or not, the finally will run. It's a good place to put code that cleans up after the try.
Obviously it won't run if you break the jvm :)