Skip to content

[java] IdenticalCatchBranches: False negative for overriden method calls #6079

@zbynek

Description

@zbynek

Affects PMD Version:
7.17.0

Rule:

IdenticalCatchBranches

Description:

Two exception handling blocks are not considered equivalent if both contain e.getMessage() and one of the exception types overrides getMessage().

Code Sample demonstrating the issue:

public class Foo {
    public void bar() {
        try {
            // nothing
        } catch (NullPointerException ex) {
            System.out.println(ex.getMessage());
        } catch (CustomException ex) {
            System.out.println(ex.getMessage());
        } catch (RuntimeException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static class CustomException extends RuntimeException {
        @Override public String getMessage() {
            return "";
        }
    }
}

Expected outcome:

PMD should report a violation at lines 7 and 9, but doesn't. This is a false-negative.
In Java < 17 only the second violation is missing because NPE does not override getMessage.

Running PMD through: IntelliJ

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions