43 lines
821 B
Plaintext
43 lines
821 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;
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|