rspec/rules/S1193/java/rule.adoc

54 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
try {
/* ... */
} catch (Exception e) {
if(e instanceof IOException) { /* ... */ } // Noncompliant
if(e instanceof NullPointerException{ /* ... */ } // Noncompliant
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
try {
/* ... */
} catch (IOException e) { /* ... */ } // Compliant
} catch (NullPointerException e) { /* ... */ } // Compliant
----
== Resources
2021-04-28 16:49:39 +02:00
* 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[]