Skip to content

[java] AvoidThrowingNewInstanceOfSameException: message inconsistent with logic #6844

Closed

Description

@mernst

Affects PMD Version: 7.26.0

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

Description: PMD's error message is inconsistent with PMD's logic.

Code Sample demonstrating the issue:

  public static MethodTree parseMethod(String methodSource) {
    Tree member;
    try {
      member = parseTypeMember(methodSource);
    } catch (IllegalArgumentException e) {
      throw new IllegalArgumentException("Invalid method: " + methodSource);
    }
    if (!(member instanceof MethodTree mt)) {
      throw new IllegalArgumentException("Invalid method: " + methodSource);
    }
    return mt;
  }

Expected outcome:

PMD reports a violation with message "A catch statement that catches an exception only to wrap it in a new instance of the same type of exception and throw it should be avoided". The given code does not wrap the caught exception. The code does create a new instance. Please do one of the following; I prefer the former:

  • change the logic to only issue the error if the newly-thrown exception wraps the original one, or
  • change the error message.

Running PMD through: Gradle

Activity

  1. added
    a:false-positive
    on Jul 6, 2026
  2. added this to the 7.27.0 milestone on Jul 9, 2026
  3. added a commit that references this issue on Jul 9, 2026
    497eabe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions