Either log or rethrow this exception. (squid:S1166): support ignoring the exception

1,969 views
Skip to first unread message

Eduard Wirch

unread,
Sep 7, 2016, 2:13:58 AM9/7/16
to SonarQube
There are cases where a exception is ignored intentionally. There should be a intuitive way to mark such cases (I generally don't like //nosonar comments in the code). In IntelliJ IDEA it's implemented this way: if the exception parameter name is "ignored", then IntelliJ accepts this as a conscious decision to ignore the exception and doesn't mark the case. It'd be nice if squid:S1166 would support the same way of ignoring:

} catch (final NumberFormatException ignored) {
   return DEFAULT_VALUE;
}


Julien Herr

unread,
Sep 9, 2016, 5:23:19 PM9/9/16
to SonarQube
It is supposed to be already the case.

Exceptions

InterruptedExceptionNumberFormatExceptionParseException and MalformedURLException exceptions are arguably used to indicate nonexceptional outcomes.

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.

int myInteger;
try {
  myInteger = Integer.parseInt(myString);
} catch (NumberFormatException e) {
  // It is perfectly acceptable to not handle "e" here
  myInteger = 0;
}

Michael Gumowski

unread,
Sep 12, 2016, 5:09:10 AM9/12/16
to Julien Herr, SonarQube
Hello Eduard,

As pointed by @Julien, by default and if the rule configuration has not been changed, the rule will ignore the following exceptions when unused:
  • java.lang.InterruptedException,
  • java.lang.NumberFormatException,
  • java.text.ParseException,
  • java.net.MalformedURLException
Consequently, your example won't raise any issue, even if the variable name of the exception was not "ignored". Couldn't you add the exception types you want to ignore to that list?

From our point of view, however, we prefer to mark such issues as "Won't Fix" in SonarQube (SQ). It's still an issue, outside the code, which can be reviewed externally.

If you are using SonarLint and are annoyed by the issues still appearing in your code while being marked as won't fix in SQ, note that a feature which should be delivered soon will be to ignore  such issues when using connected mode (MMF-441).

Regards,
 

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/70397959-9dbc-489c-bdf9-3a588ad657cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Michael GUMOWSKI | SonarSource
Software Developer @ Language Team
http://sonarsource.com
Reply all
Reply to author
Forward
0 new messages