rspec/rules/S1166/exceptions.adoc

29 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

=== Exceptions
2021-01-27 13:42:22 +01:00
``++InterruptedException++``, ``++NumberFormatException++``, ``++DateTimeParseException++``, ``++ParseException++`` and ``++MalformedURLException++`` exceptions are arguably used to indicate nonexceptional outcomes. Similarly, handling ``++NoSuchMethodException++`` is often required when dealing with the Java reflection API.
2021-02-02 15:02:10 +01:00
Because they are part of Java, developers have no choice but to deal with them. This rule does not verify that those particular exceptions are correctly handled.
2021-02-02 15:02:10 +01:00
----
int myInteger;
try {
myInteger = Integer.parseInt(myString);
} catch (NumberFormatException e) {
// It is perfectly acceptable to not handle "e" here
myInteger = 0;
}
----
Furthermore, no issue will be raised if the exception message is logged with additional information, as it shows that the developer added some context to the error message.
2021-02-02 15:02:10 +01:00
----
try {
/* ... */
} catch (Exception e) {
String message = "Exception raised while authenticating user: " + e.getMessage();
LOGGER.warn(message); // Compliant - exception message logged with some contextual information
}
----