Split S1166 to two rules: logger vs. new exception

java

(Adam Gabryś) #1

Hello,
Is it possible to split S1166 Exception handlers should preserve the original exceptions to two rules? First will be applied when logger is used and second when developer creates a new exception. Why? Let’s use an example:

try {
  /* ... */
} catch (Exception e) {
  LOGGER.warn(e.getMessage());
}
doSomething()

try {
  /* ... */
} catch (Exception e) {
  throw new RuntimeException("context");
}
doSomething2()

Usually, at first case we consciously hide the stacktrace. We just want to inform that something was wrong, but next we continue the standard flow. At second case we break the flow ( doSomething2 is not executed) and hide the real cause. If somebody wants to:

  • fix the problem - he/she won’t know what is broken
  • ignore the exception - he/she may log warn message (see case no 1)

From our point of view first case is minor/major and second is critical/blocker. Also some security tools forbidden put stacktraces in logs (sensitive information may leak). For example we use HP Fortify which provides a such rule. We have clash problems here :disappointed:

What do you think?

Cheers