Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: elim 1st NCCE/CS set

...

This noncompliant code example uses an ordinary try-finally block to try to close two resources.  However, if closing the BufferedReader br results in an exception being thrown, then the BufferedWriter bw will not be closed.

Code Block
bgColor#FFcccc
public void processFile(String inPath, String outPath) throws IOException{
	BufferedReader br = null;
    BufferedWriter bw = null;
    try {
    	br = new BufferedReader(new FileReader(inPath));
        bw = new BufferedWriter(new FileWriter(outPath));
        // process the input and produce the output
    } finally {
    	if (br != null) {
        	br.close();
        }
        if (bw != null) {
        	bw.close();
        }
    }
}

Compliant Solution (finally block)

This compliant solution uses finally blocks to guarantee that both br and bw are properly closed, regardless of any exceptions that may be thrown during the close operations.

Code Block
bgColor#ccccff
public void processFile(String inPath, String outPath) throws IOException {
  BufferedReader br = null;
  BufferedWriter bw = null;
  try {
    br = new BufferedReader(new FileReader(inPath));
    bw = new BufferedWriter(new FileWriter(outPath));
    // ... process the input and produce the output
  } finally {
    if (br != null) {
      try {
        br.close();
      } catch (IOException x) {
        // handle error
      } finally {
        if (bw != null) {
          try {
            bw.close();
          } catch (IOException x) {
            // handle error
          }
        }
      }
    }
  }
}

Compliant Solution (try-with-resources)

This compliant solution uses a try-with-resources statement which will guarantee that both br and bw are closed, regardless of any exceptions potentially thrown during the close operations.

Code Block
bgColor#ccccff
public void processFile(String inPath, String outPath) throws IOException{
	try (BufferedReader br = new BufferedReader(new FileReader(inPath));
         BufferedWriter bw = new BufferedWriter(new FileWriter(outPath));) {
            // process the input and produce the output
        }
}

Noncompliant Code Example

This noncompliant code example uses an ordinary try-finally block to try to close a resource.  However, if there is an exception thrown during the processing of the input and another exception thrown when closing the Bufferedreader br, then the exception thrown as a result of processing the input will be lost, and important information about that exceptional circumstance may be missed.

Code Block
bgColor#FFcccc
public void processFile(String inPath) throws IOException{
  BufferedReader br = null;
  try {
    br = new BufferedReader(new FileReader(inPath));
    // process the input and produce the output
    } finally {
      if (br != null) {
        br.close();
      }
    }
  }
}

Compliant Solution (try-with-resources)

This compliant solution uses a try-with-resources statement which will not suppress any exceptions thrown during the processing of the input while still guaranteeing that br is closed. It demonstrates how to access every exception that may be produced from the try-with-resources block.

...