TL;dr version: (Java) How to check for unused Exception variables in catch clauses to avoid unintentionally swallowing errors. Maybe using tools like checkstyle (through Maven build time plugin) or using a code pattern.
Quite often in your code you write a try/catch block where u catch an internal exception, wrap it in a more meaningful exception (chained exception) and throw upwards in the call stack.
At times, people tend to forget to chain the exception (typos) and throw the wrapper exception without the underlying cause. Missing this out obviously causes debugging nightmares. Is there a way through build time plugins (for Maven) that can detect and warn/error on such instances?
Valid:
try {
...
} catch (IOException e) {
throw new MyException("API failure", e);
}
Invalid:
try {
...
} catch (IOException e) {
throw new MyException ("API Failure");
}
Also, in case the it is intended to ignore the exception, the checker should be able to allow for a comment to disable rule for that line or maybe name the variable as catch (IOException ignored).
I know checkstyle has a checker for empty catch block, but the above scenario is much broader. Unused local variables doesn't seem to solve it either.
Are there any check tools available to avoid this common mistake?
One dumb solution could be to make all constructors of MyException take Exception as an argument (when no chaining required, pass null explicitly). This doesn't feel like the best way to go about this, hence looking for some kind of checker...