A catch
clause that only rethrows the caught exception has the same effect as omitting the catch
altogether and letting
it bubble up automatically.
public String readFile(File f) throws IOException {
String content;
try {
content = readFromDisk(f);
} catch (IOException e) {
throw e;
}
return content;
}
Such clauses should either be removed or populated with the appropriate logic.
public String readFile(File f) throws IOException {
return readFromDisk(f);
}
or
public String readFile(File f) throws IOException {
String content;
try {
content = readFromDisk(f);
} catch (IOException e) {
logger.LogError(e);
throw e;
}
return content;
}
In the case of try-with-resources, the try should remain even without a catch clause, to keep the resource management
String readFirstLine(FileReader fileReader) throws IOException {
try (BufferedReader br = new BufferedReader(fileReader)) {
return br.readLine();
} catch (IOException e) { // Noncompliant
throw e;
}
becomes
String readFirstLine(FileReader fileReader) throws IOException {
try (BufferedReader br = new BufferedReader(fileReader)) {
return br.readLine();
}
}