38 lines
921 B
Plaintext
38 lines
921 B
Plaintext
Multiple catch blocks of the appropriate type should be used instead of catching a general exception, and then testing on the type.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
try {
|
|
/* ... */
|
|
} catch (Exception e) {
|
|
if(e instanceof IOException) { /* ... */ } // Noncompliant
|
|
if(e instanceof NullPointerException{ /* ... */ } // Noncompliant
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
try {
|
|
/* ... */
|
|
} catch (IOException e) { /* ... */ } // Compliant
|
|
} catch (NullPointerException e) { /* ... */ } // Compliant
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* https://wiki.sei.cmu.edu/confluence/display/java/ERR51-J.+Prefer+user-defined+exceptions+over+more+general+exception+types[CERT, ERR51-J.] - Prefer user-defined exceptions over more general exception types
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|