Question

class Demo {
    public static void main(String args[]) {
        System.out.println("Start main");
        try {
            //exceptional code
            int x=43/0;
        } catch(ArithmeticException e) {
            e.printStackTrace();
        } finally {
            System.out.println("final code");
        }
        System.out.println("End main");
    }
}

I used the above code to understand the mechanism of a finally block. In this case, what I observed is that even without that catch block, the finally block executes with showing the exception occurred. But the difference I observed is that the "End main" is not printed when the catch block is not used. I want to know the reason why the finally block executes even if the exception is not handled by using a catch clause. And I like to know what the basic functionality of a finally block is.

Was it helpful?

Solution

Finally blocks are used to run the code, irrespective of the fact whether there is an exception or not. Finally always executes for a related try block.

Generally finally is used to close the resources opened in a try block. The reason to do so in finally is that, no matter whether there is an exception or not , we should close the resoruce. But in Java 7 you really don't need to bother with that, if you use try with resource for an AutoCloseable resource.

In your code, the relevance of finally is nothing except about learning how finally works.

Learn more about finally : http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

and a note from the same link:

Important: The finally block is a key tool for preventing resource leaks. When closing a file or otherwise recovering resources, place the code in a finally block to ensure that resource is always recovered.

If you are using Java SE 7 or later, consider using the try-with-resources statement in these situations, which automatically releases system resources when no longer needed.

OTHER TIPS

Whatever happens on try or catch blocks, the finally cause ensures* for you that will be executed.

*The finally block won't be executed only if you interrupt or kill the current Thread when it's running in the try / catch block.

Here is a more specific docs.

From the Java Tutorials:

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

So if you had to guarantee an action being performed, such as somePrintWriter.close(), but want to still handle any exceptions before then, the finally block would execute it.

Either the try-catch-finally block -or- the try-with-resources block is used in Java to achieve a similar functionality to object finalizers in C++.

While they are used in exception processing, probably the most important function of the finally statement block is to close or free up resources that have been opened or created during the execution of code in the try block.

Note that object finalizers exist (after a fashion) in Java, but they should almost never be used to finalize objects ... weird, eh?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top