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.
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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With