rspec/rules/S2221/java/rule.adoc
Fred Tingaud 51369b610e
Make sure that includes are always surrounded by empty lines (#2270)
When an include is not surrounded by empty lines, its content is inlined
on the same line as the adjacent content. That can lead to broken tags
and other display issues.
This PR fixes all such includes and introduces a validation step that
forbids introducing the same problem again.
2023-06-22 10:38:01 +02:00

65 lines
1.5 KiB
Plaintext

== Why is this an issue?
Catching ``++Exception++`` seems like an efficient way to handle multiple possible exceptions. Unfortunately, it traps all exception types, both checked and runtime exceptions, thereby casting too broad a net. Indeed, was it really the intention of developers to also catch runtime exceptions? To prevent any misunderstanding, if both checked and runtime exceptions are really expected to be caught, they should be explicitly listed in the ``++catch++`` clause.
This rule raises an issue if ``++Exception++`` is caught when it is not explicitly thrown by a method in the ``++try++`` block.
=== Noncompliant code example
[source,java]
----
try {
// do something that might throw an UnsupportedDataTypeException or UnsupportedEncodingException
} catch (Exception e) { // Noncompliant
// log exception ...
}
----
=== Compliant solution
[source,java]
----
try {
// do something
} catch (UnsupportedEncodingException|UnsupportedDataTypeException|RuntimeException e) {
// log exception ...
}
----
or if runtime exceptions should not be caught:
[source,java]
----
try {
// do something
} catch (UnsupportedEncodingException|UnsupportedDataTypeException e) {
// log exception ...
}
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Catch a list of specific exception subtypes instead.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]