Is resource closure accepted in a try block?

The Java beginners book contains the following code. This book also explains exceptions very well, and since I understood how the exception works, I had a question about the following code.

For some reason, if the FileWriter class throws an exception, writer.close()it will not be executed. Therefore, I believe that the best place to close the writer object is in the final block. Even before that, I saw a lot of code written in this way, where the resource will be closed in the try block itself. I think there is no point in this. Only when there is no exception, the resource will be closed.

Am I really wrong? What is the best way to close resources in java. Should we write code as follows?

 public static void main(String[] args) {

       try{
         FileWriter writer = new FileWriter("file.txt");
         writer.write("i am writing");
         writer.close();
       }catch(IOException e){
           ex.printStackTrace();
       }

    }
+4
6

@cyber-rookie, , finally.

Java 7 "try-with-resources", ...

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

...

    try (FileWriter writer = new FileWriter("file.txt")) {
        writer.write("i am writing");
    } catch (IOException e) {
        e.printStackTrace();
    }

,

+3

Java 7, try . . https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

 try (FileWriter writer = new FileWriter("file.txt")) {
   writer.write("i am writing");
 }
+2

, finally.

java 7 try-with-resource :

try (BufferedReader br =
               new BufferedReader(new FileReader(path))) {
    return br.readLine();
}

BufferedReader try-with-resource, , try normally or abruptly, AutoCloseable.

+1
source

In my experience, we would use a finallytry-catch clause :

public static void main(String[] args) {

    FileWriter writer = null;
    try {
        writer = new FileWriter("file.txt");
        writer.write("i am writing");

    } catch (IOException ex) {
        ex.printStackTrace();

    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}

PS: Put this in a separate method and throw an exception, let the class use these descriptor exceptions.

+1
source

Reply to comment to add multiple resources in an attempt with a resource block:

try(FileWriter writer = new FileWriter("file.txt"); BufferedReader reader = new BufferedReader(new FileReader("file.txt"))){
        // you can put many AUTOCLOSEABLE objects in try with resource. Just seperate them with ";"  
    } catch (IOException e) {
        e.printStackTrace();
    }
+1
source

At work (Java 6), we close the resources in the TRY block, and then close the protection in the FINALLY block.

BufferedReader bufferedReader;
try {
  //initialize and do something with the bufferedReader
   bufferedReader.close();
} catch (FileNotFoundException ex) {
    // notify the user 
} catch (IOException ex) {
    // notify the user 
} finally {
    if (bufferedReader != null) {
        try {
           //defensive close
            bufferedReader.close();
        } catch (IOException ex) {
            // this will be thrown if bufferedReader is already closed (in Try block, so can be leave to blank

        }
    }
}
+1
source

All Articles