2

Will the following finally clause be executed, if an exception is thrown by the PrintWriter?

try{
   PrintWriter out = new PrintWriter(filename);
   try {
       //output
   } finally {
       out.close();
   }
} catch {
   //handle exception
}

If the PrintWriter throws an exception, then the nested try block will never get executed, but why the nested finally clause will still be executed, even it's nested and skipped?

Updates: I ran some tests, if an exception is thrown before the nested try clause, that nested finally will not be executed. If the exception is thrown inside the nested try clause, then the inner finally and the outter catch will be executed.

4
  • 7
    That finally block is not related to your print writer statement Commented Jan 8, 2020 at 9:36
  • 3
    You should take a look at docs.oracle.com/javase/tutorial/essential/exceptions/… btw. Commented Jan 8, 2020 at 9:38
  • PrintWriter implements AutoCloseable and therefore, you can just use a try with resources like try (PrintWriter out = new PrintWriter(filename)) { ... } catch { ... } where no finally will be needed (for out). What's inside the inner try that could throw an Exception? Commented Jan 8, 2020 at 9:43
  • it will in fact NOT be executed, but the inevitable application crash will cause the operating system to close the file (if it were even created)... Commented Jan 8, 2020 at 10:00

3 Answers 3

4

No because the inner try block will not be reached when an exception occurs before and therefore the finally block is not reached either.

Sign up to request clarification or add additional context in comments.

Comments

1

Finally block is always executed whether exception is handled or not. Even though their is an error and it reaches to catch block, it will go to finally block to execute the piece of code.

finally block is a block that is used to execute important code such as closing connection, stream etc.

So, Inside try{} block you placed try and finally, but you asked about the catch of outside try ,thus its not going inside the first try block.That finally wont work.

P.S. : If you put finally something like this:

     try{
          try{...}
          finally{...}
        }catch(Exception e){...}
        finally{... }
//in case of exception also , the outside finally is going to work.

P.S.: Though you got your answer , but the concept is for reference of other naive programmers

Comments

0

An uglier variant (sometimes generated by the IDE) one sees also:

// *** UGLY
PrintWriter out = null;
try {
    out = new PrintWriter(filename);
    //output
} catch (IOException e) {
    //handle exception
} finally {
    if (out != null) {
        try {
            out.close();
        } catch (IOException e2) {
            // IGNORE
        }
    }
}

That explains the code a bit: as close may throw an IOException too, the code becomes cumbersome. Your code still needs nested exceptions.

With try-with-resources this can & should be written as:

try (PrintWriter out = new PrintWriter(filename)) {
    //output
} catch (IOException e) {
    //handle exception
} // Automatically closes.

And no longer nested exceptions.

The biggest advantage is that you need not catch any exception, and just add a throws IOException in the method signature.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.