Why finally useful in java?

Wondering why finally

useful after a test try catch

? In either case, the code defined after the statement finally

will be executed.

What is the difference between these two codes?

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

      

and:

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

      

Even if the error is caught, "It me again..."

..

+3


source to share


3 answers


Good for starters, if the stream is closed and the block thus throws an exception, then the finally block will still be executed. Thus, they are not equivalent . So, in context: System.out

catch

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

      

Finally, let's try to write at least out

. It won't happen if you write it after the try

- block catch

.

finally

useful for several reasons:

  • Usually the chance that the code in the block try

    throws another exception that is specified in the blockfinally

    will be executed in this case .

  • if try

    there is a / / operator in the block that acts outside the block (for example, in a loop ), it will not be executed, if you write it after , then it will be called . This also leads to more elegant code. For example: return

    break

    continue

    try

    break

    try

    for

    try

    finally

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

    Pay attention to the instruction return

    : if you make a close

    file without finally

    , you will need to define a variable, etc. (moreover, if something fails when reading the file, of course, it will not close the file).

  • if the block throws an exception on its way , it will finally be executed again. catch



It also allows you to do some things after an exception has been thrown, but it doesn't catch the exception: the exception is added additionally if it is never caught. When:

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

      

Constructed SomeException

and its stacktrace are not "crossed out" by the block finally

: the stack trace is not changed. This way the bugfixer can find out where the original exception was thrown from.

In general, it is good to write everything that needs to be done before leaving try-catch

(with zero or more catch

es) a block in finally

, as it will protect you from all kinds of corner cases.

More in programming language theory, return

, break

, continue

, throw

, etc. - all mechanisms for changing the code. A finally

declares that in the event of such behavior, you are protected. If the Java developers later introduce a new mechanism, your code will still be "protected". It is always advisable to use the infrastructure provided by a programming language, as developers consider all of these possibilities.

+5


source


From docs

This ensures that the finally block is executed even if an unexpected exception occurs.



Pay attention to the term unexpected . You are expected to throw an exception.

+1


source


The finally block is usually used to perform final operations, even if some exception appears in the middle of your code.

An example would be closing a file in a finally block.

0


source







All Articles