pmd / pmd

An extensible multilanguage static code analyzer.

Home Page:https://pmd.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[java] AvoidRethrowingException should allow rethrowing exception subclasses

cowwoc opened this issue · comments

Affects PMD Version: 7.0.0

Rule: https://docs.pmd-code.org/latest/pmd_rules_java_design.html#avoidrethrowingexception

Description:
AvoidRethrowingException is meant to catch unnecessary catch blocks that simply rethrow the exception. However, there is an edge case that is not considered by the rule.

Code Sample demonstrating the issue:

try
{
  Files.delete("example.txt");
}
catch (NoSuchFileException e)
{
  // Unrecoverable error
  throw e;
}
catch (IOException e)
{
  // Potentially recoverable error. Sleep and try again...
}

Expected outcome:
In the above example, one must catch and rethrow NoSuchFileException in order to avoid being caught by the IOException handler. Sometimes it is possible to refactor the code to avoid this, but sometimes it is not.

Please update the rule to ignore catching and rethrowing an exception if a later catch matches the exception's superclass.

Running PMD through: Maven