rspec/rules/S3472/java/rule.adoc
2021-04-28 18:08:03 +02:00

35 lines
660 B
Plaintext

Returning ``++null++`` when something goes wrong instead of throwing an exception leaves callers with no understanding of what went wrong. Instead, an exception should be thrown.
== Noncompliant Code Example
----
public MyClass readFile(String fileName) {
MyClass mc;
try {
// read object from file
} catch (IOException e) {
// do cleanup
return null; // Noncompliant; why did this fail?
}
return mc;
}
----
== Compliant Solution
----
public MyClass readFile(String fileName) throws IOException{
MyClass mc;
try {
// read object from file
} catch (IOException e) {
// do cleanup
throw e;
}
return mc;
}
----