Details about the known misuse from the MUBench dataset.
| Description: | Calls Closeable.close() outside of finally block, which might fail to close the resource in case of an exception.
|
| Fix Description: |
Call close() in finally block.
|
| Violation Types: |
- missing/exception handling
|
| In File: | CloseFinally.java |
| In Method: | misuse(File) |
| Code with Misuse: |
class CloseFinally {
public void misuse(File file) throws IOException {
Writer writer = new PrintWriter(new FileOutputStream(file));
writer.write("foo");
writer.close();
}
}
|
| Code with Pattern(s): |
public class TryFinallyClose {
public void pattern(OutputStream out, String value) throws IOException {
Writer writer = null;
try {
writer = new PrintWriter(out);
writer.write(value);
} finally {
if (writer != null) {
writer.close();
}
}
}
}
|