rspec/rules/S1193/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

54 lines
1.2 KiB
Plaintext

== Why is this an issue?
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
[source,java]
----
try {
/* ... */
} catch (Exception e) {
if(e instanceof IOException) { /* ... */ } // Noncompliant
if(e instanceof NullPointerException{ /* ... */ } // Noncompliant
}
----
=== Compliant solution
[source,java]
----
try {
/* ... */
} catch (IOException e) { /* ... */ } // Compliant
} catch (NullPointerException e) { /* ... */ } // Compliant
----
== Resources
* 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[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace the usage of the "instanceof" operator by a catch block.
'''
== Comments And Links
(visible only on this page)
=== on 16 Aug 2013, 08:27:16 Freddy Mallet wrote:
Is implemented by \http://jira.codehaus.org/browse/SONARJAVA-292
endif::env-github,rspecator-view[]