Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] AvoidRethrowingException should allow rethrowing exception subclasses #4911

Open
cowwoc opened this issue Apr 2, 2024 · 0 comments
Open
Labels
a:false-positive PMD flags a piece of code that is not problematic

Comments

@cowwoc
Copy link

cowwoc commented Apr 2, 2024

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

@cowwoc cowwoc added the a:false-positive PMD flags a piece of code that is not problematic label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

No branches or pull requests

1 participant