GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Merge AvoidCatchingNPE and AvoidCatchingThrowable into AvoidCatchingGenericException · Issue #6038 · pmd/pmd

[java] Merge AvoidCatchingNPE and AvoidCatchingThrowable into AvoidCatchingGenericException #6038

New issue
Copy link
New issue
Copy link
Closed
#6051
Closed
[java] Merge AvoidCatchingNPE and AvoidCatchingThrowable into AvoidCatchingGenericException#6038
#6051
Copy link
Labels
an:enhancementAn improvement on existing features / rulesis:deprecationThe main focus is deprecating public APIs or rules, eg to make them internal, or removing them
Milestone
7.18.0

Description

@UncleOwen
UncleOwen
opened on Sep 6, 2025
Issue body actions

I just noticed that these three rules overlap

Design/AvoidCatchingGenericException
ErrorProne/AvoidCatchingNPE
ErrorProne/AvoidCatchingThrowable

Therefore I suggest to

  1. Make the list of exceptions in AvoidCatchingGenericException configurable
  2. Add java.lang.Throwable and java.lang.Error to the default list
  3. Deprecate (and remove in the next major version) the other 2 exceptions.

Activity

  1. UncleOwen
    added
    an:enhancementAn improvement on existing features / rules
    is:deprecationThe main focus is deprecating public APIs or rules, eg to make them internal, or removing them
    and removed
    is:deprecationThe main focus is deprecating public APIs or rules, eg to make them internal, or removing them
    on Sep 6, 2025
  2. adangel commented on Sep 25, 2025

    @adangel
    adangel
    on Sep 25, 2025
    Member
    More actions

    Thanks for the suggestion. It makes sense to merge these rules.

    The only point I unsure about is the final category - should it be design or error prone?
    I'm currently leaning towards error prone...

    Reacted by Clément Fournier
  3. zbynek commented on Sep 26, 2025

    @zbynek
    zbynek
    on Sep 26, 2025
    Contributor
    More actions

    I wonder if the "avoid catching generic exceptions" fits all the usecases here.

    The issue with catching Throwable, Error and Exception is that the catch block is too generic, so far so good.

    The issue with NPEs is that they may hide programming errors and one should use null checks instead. For the same reason it's better not to catch IndexOutOfBoundsExceptions (use index checks instead) and ClassCastExceptions (use instanceof intead).

    Maybe the name could avoid the word "generic", st like AvoidCatchingUncheckedThrowable? AvoidCatchingCommonException?

    BTW there is a similar SpotBugs rule: https://spotbugs.readthedocs.io/en/stable/bugDescriptions.html#rec-exception-is-caught-when-exception-is-not-thrown-rec-catch-exception

  4. UncleOwen commented on Sep 27, 2025

    @UncleOwen
    UncleOwen
    on Sep 27, 2025
    MemberAuthor
    More actions

    If we're changing the exception name, the new name shouldn't tell the user what to do. Something like CaughtExceptionIsOverlyBroad. But that doesn't address @zbynek 's valid concerns. CaughtExceptionHidesProgrammingError? Hmm. Maybe these should be two separate rules? Both configurable, both with the same implementation?

  5. adangel commented on Oct 23, 2025

    @adangel
    adangel
    on Oct 23, 2025
    Member
    More actions

    I'm going to keep the name AvoidCatchingGenericException for now. E.g. CWE-396 also uses the term "generic". If we come up with a better name, we can rename it later anyways. The different use cases a now much better described in the new rule description - and I think, this covers all the cases.

    I'll move the rule however from "Design" to "Error Prone", as it is simply dangerous and error prone to do such an error handling...

  6. adangel
    added
    is:deprecationThe main focus is deprecating public APIs or rules, eg to make them internal, or removing them
    on Oct 23, 2025
  7. adangel
    added a commit that references this issue on Oct 23, 2025

    [doc] Update release notes (pmd#6051, pmd#6038)

    3f952d0
  8. adangel
    added this to the 7.18.0 milestone on Oct 23, 2025
  9. adangel
    closed this as completedin #6051on Oct 24, 2025
  10. adangel
    added a commit that references this issue on Oct 24, 2025

    [java] Fix pmd#6038: Make AvoidCatchingGenericException configurable (p…

    This issue will close once commit b6d500b is merged into the 'main' branch.
    b6d500b
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

    an:enhancementAn improvement on existing features / rulesis:deprecationThe main focus is deprecating public APIs or rules, eg to make them internal, or removing them

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.18.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.