Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is finally useful in java? [duplicate]

I wonder why finally is useful after a try catch test? In any scenario, the code defined after the finally statement will be executed.

What is the difference with these two codes?

try{
    int a = 1 / 0;
} catch(ArithmeticException e){
    System.out.print("Hi");
} finally {
    System.out.print("It's me again...");
}

and :

    try{
    int a = 1 / 0;
} catch(ArithmeticException e){
    System.out.print("Hi");
}
System.out.print("It's me again...");

Even if a error is catch, "It's me again..." will be displayed..

like image 964
M.Ferru Avatar asked Mar 10 '23 02:03

M.Ferru


1 Answers

Well for starters, if the System.out stream is closed, and the catch block thus raises an exception, then the finally block will still be executed. So the two are not equivalent. So in the context of:

System.out.close();
try{
    int a = 1 / 0;
} catch(ArithmeticException e){
    System.out.print("Hi");
} finally {
    System.out.print("It's me again...");
}

Finally will at least do an attempt to write to out. This won't be the case if you write it after the try-catch block.

The finally is useful for a couple of reasons:

  1. It is usually a possibility that the code in the try block raises another exception that the one specified in the catch block(s), in that case finally will still be executed.

  2. if there is a return/break/continue statement in the try block that has effect outside the try block (e.g. a break in a try in a for loop), it will not be executed if you write it after the try, then finally will also be called. This also results in more elegant code. For instance:

    BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    
    try {
        return br.readLine();
    } finally {
        br.close();
    }

    Notice the return statement: if you would do the close of the file without a finally, it would require you to define a variable, etc. (furthermore if something goes wrong when reading the file, it will of course not close the file).

  3. if the catch block(s) on their turn throw an exception, again finally will be executed.

It also allows you to perform some actions after an exception has been thrown, but without catching the exception: the exception is thrown further if is never catched. In case of:

try {
    throw new SomeException("The culprit!");
} finally {
    System.out.println("Some closing words.");
}

The constructed SomeException and its stacktrace is not "toched" by the finally block: the stack trace is not altered. So the bugfixer can find out where the exception has been thrown originally.

In general it is good to write everything that has to be done before leaving the try-catch (with zero or more catches) block in a finally, since it protects you against all kinds of corner cases.

More into programming language theory, return, break, continue, throw, etc. are all code path changing mechanisms. A finally statement guards that in case of such behaviour you are protected. If later the designers of Java would introduce a new mechanism your code is still "protected". It is always advisable to use infrastructure a programming language offers since the designers take all these possibilities into account.

like image 121
Willem Van Onsem Avatar answered Mar 31 '23 07:03

Willem Van Onsem