GeezerVison is showing github.com — [java] Merge AvoidCatchingNPE and AvoidCatchingThrowable into AvoidCatchingGenericException · Issue #6038 · pmd/pmd
[java] Merge AvoidCatchingNPE and AvoidCatchingThrowable into AvoidCatchingGenericException #6038Labels an:enhancementAn improvement on existing features / rulesis:deprecationThe main focus is deprecating public APIs or rules, eg to make them internal, or removing them Activityadded 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, 2025adangel commented on Sep 25, 2025 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 zbynek commented on Sep 26, 2025 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 UncleOwen commented on Sep 27, 2025 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? adangel commented on Oct 23, 2025 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... added a commit that references this issue on Oct 24, 2025This issue will close once commit b6d500b is merged into the 'main' branch.b6d500b
MetadataMetadataan:enhancementAn improvement on existing features / rulesis:deprecationThe main focus is deprecating public APIs or rules, eg to make them internal, or removing them DevelopmentNo branches or pull requestsIssue actions
|