Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finally sometimes confuse me [duplicate]

Tags:

Today in college we talked a little bit about try, catch and finally. I got confused about these two examples:

PrintWriter out = null; try {   out = new PrintWriter(...); // We open file here } catch (Exception e) {   e.printStackTrace(); } finally { // And we close it here   out.close(); } 

What is the difference between closing the file in finally and if we just did it this way:

PrintWriter out = null; try {   out = new PrintWriter(...); // We open file here } catch (Exception e) {   e.printStackTrace(); } out.close(); 

This piece of code after catch will always execute.

Can you give me some good examples about the differences between when we use finally and when we put the code after catch? I know that finally will always execute, but the program will also keep running after catch block.

like image 613
Miljan Rakita Avatar asked Apr 26 '16 13:04

Miljan Rakita


People also ask

What does confusion feel like?

Confusion is the inability to think as clearly or quickly as you normally do. You may feel disoriented and have difficulty paying attention, remembering, and making decisions.

How do you manage confusion?

There are a number of things you can do to help prevent confusion: Don't drink too much alcohol. Avoid recreational (illegal) drugs. Eat a healthy, balanced diet with plenty of fresh fruit and vegetables to prevent vitamin or mineral deficiencies.


2 Answers

It would still make difference if the code throws Error. This is not caught within the code and therefore any part after try/catch/finally wouldn't be caught. If it's part of finally, it will be still executed even for Error.

Secondly, if for whatever reason e.printStackTrace() throws an exception (although it would be very rare), the same will happen - finally will be still executed.

In general finally is very safe way for releasing resources no matter what happens. Even more safe is try-with-resources supported since Java 7 as it could easily manage possibly multiple exceptions thrown during close operations. In this example it would look like:

try (PrintWriter out = new PrintWriter(...)) {     // do whatever with out } catch (Exception e) {     e.print... (whatever) } // no need to do anything else, close is invoked automatically by try block 

EDIT: Also note that your code is not really correct (no matter which version). If the PrintWriter constructor throws an exception, the line out.close() will fail on NullPointerException.

like image 137
Zbynek Vyskovsky - kvr000 Avatar answered Sep 25 '22 21:09

Zbynek Vyskovsky - kvr000


If an uncaught error occurs within the try block, or even if an error occurs within your catch block, the 'piece of code' after the catch won't be executed, but the finally block will.

finally will always be executed.

From the Java documentation :

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.

like image 44
Guillaume Barré Avatar answered Sep 22 '22 21:09

Guillaume Barré