Friday, January 18, 2019

try-with-resource vs. try-catch-finally

The try-with-resource was introduced to java in JDK1.7. While both try-with-resource and the finally blocks guarantee to close the resources, the differences between them are subtle.  

1. try-catch-finally

public void copyA(String source, String destination) throws FileNotFoundException, IOException {
      BufferedReader reader = null;
      PrintWriter writer = null;
      try {
            reader = new BufferedReader(new FileReader("source"));
            writer = new PrintWriter(new BufferedWriter(new FileWriter("destination")));

            String line = null;
            while((line = reader.readLine()) != null) {
                  writer.printLine(line);
             }
            writer.flush();
      } finally {
            if (reader != null) reader.close();
            if (writer != null) writer.close();
      }
}

 
2. try-with-resource

public void copyB(String source, String destination) throws FileNotFoundException, IOException {
      try (BufferedReader reader = new BufferedReader(new FileReader("source"));
                  PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("destination")))){
            String line = null;
            while((line = reader.readLine()) != null) {
                  writer.printLine(line);
             }
            writer.flush();
}

Both copyA and copyB will close the reader and writer either the code is executed successfully or throws exceptions.

If an exception occurs in the try block, with try-with-resource, method copyB will always throw the original exception from the try block. With the try-finally block, if the reader and writer are closed successfully without error, method copyA will also throw the original exception from the try block. However, if either the reader or the writer failed to close and throws IOException, copyA will throw the IOException from the finally block instead the one from the try block, which is known as EXCEPTION MASKING.

One way to avoid exception masking is to handle the exceptions within the method by catching them.

public void copyA(String source, String destination) throws FileNotFoundException, IOException {
      BufferedReader reader = null;
      PrintWriter writer = null;
      try {
            reader = new BufferedReader(new FileReader("source"));
            writer = new PrintWriter(new BufferedWriter(new FileWriter("destination")));

            String line = null;
            while((line = reader.readLine()) != null) {
                  writer.printLine(line);
             }
            writer.flush();
      } catch (FileNotFoundException fe) {
            fe.printStackTrace(System.out);
      } catch(IOExcetpion ie) {
            ie.printStackTrace(System.out);
      } finally {
            try {
                 if (reader != null) reader.close();
                 if (writer != null) writer.close();
            } catch(IOExcetpion ioe) {
                  ioe.printStackTrace(System.out);
            }
      }
}


public void copyB(String source, String destination) throws FileNotFoundException, IOException {
      try (BufferedReader reader = new BufferedReader(new FileReader("source"));
                  PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("destination")))){
            String line = null;
            while((line = reader.readLine()) != null) {
                  writer.printLine(line);
             }
            writer.flush();
} catch (FileNotFoundException fe) {
      fe.printStackTrace(System.out);
} catch(IOExcetpion ie) {
      ie.printStackTrace(System.out);
}

Now, all the exceptions from all the blocks will be printed to your output. However, method copyA will handle the exception from the try block before the reader and writer are closed, and copyB will handle the exception after the reader and writer are closed.

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live


       

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

Reference:

1.The try-with-resources Statement

No comments:

Post a Comment