...
Code Block |
---|
|
public void err54_nce1processFile(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 |
---|
|
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 |
---|
|
public void err54_cs1processFile(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
}
}
|
...
Code Block |
---|
|
public void err54_nce2processFile(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.
Code Block |
---|
|
public void err54_cs2processFile(String inPath) throws IOException{
try (BufferedReader br = new BufferedReader(new FileReader(inPath));) {
// process the input and produce the output
} catch (IOException ex) {
System.err.println("thrown exception: " + ex.toString());
Throwable[] suppressed = ex.getSuppressed();
for (int i = 0; i < suppressed.length; i++) {
System.err.println("suppressed exception: "
+ suppressed[i].toString());
}
}
} |
If only one exception is thrown, either during opening, processing, or closing of the file, it will be printed by the "thrown exception: "
statement. If an exception is thrown during processing, and another one is thrown while trying to close the file, then the "thrown exception: "
statement will print the exception encountered while closing the file, and the "suppressed exception: "
statement will print the exception encountered during processing.
...