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] Fix #4815: ExceptionAsFlowControl: stop at a lambda body by Eljees · Pull Request #7105 · pmd/pmd

Conversation

@Eljees

@Eljees Eljees commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Describe the PR

ExceptionAsFlowControl walks up from a throw to the enclosing body declaration and reports any try on the way whose catch would catch the thrown type. The walk did not stop at a lambda expression, so in the code from #4815

try {
    es.execute(() -> {
        try {
            unsafeDoPublish(details);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
} catch (RuntimeException e) {
    LOGGER.warn("Issue publishing notification " + details, e);
}

the throw inside the lambda was matched against the outer catch (RuntimeException e). That catch is not where the exception goes: the lambda body runs when the executor invokes it, on another thread here, and the exception propagates to that caller.

The rule already stops at a body declaration, which covers anonymous and local classes. This makes it stop at a lambda too: whoever invokes the lambda is not visible from the throw site, so the rule cannot tell whether a surrounding try catches the exception, and it now leaves that case alone. +7/-0 in ExceptionAsFlowControlRule.

Tests

Two cases in ExceptionAsFlowControl.xml:

  • the code from [java] ExceptionAsFlowControl: False positive on Lambda/asynchronous #4815: no violation. Fails on main with Expected 0 problem(s), 1 problem(s) found;
  • the same try, with a lambda in it and a throw outside the lambda: still reported (line 12, the catch), so a lambda elsewhere in the try does not switch the rule off.

ExceptionAsFlowControlTest: 9 tests, 0 failures.

Related issues

  • Fixes [java] ExceptionAsFlowControl: False positive on Lambda/asynchronous #4815

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Eljees
[java] ExceptionAsFlowControl: stop at a lambda body
aa0394c
An exception thrown in a lambda body propagates to whoever invokes the
lambda, not to the try statement around the lambda expression.

Fixes pmd#4815
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 24, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 8 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-09-26 09:33:55+00:00 for 817461b)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

UncleOwen added 4 commits September 26, 2026 11:07
@UncleOwen
Move lambda expression check into the loop header.
18a40c8
@UncleOwen
formatting
1ab4b3a
@UncleOwen
Merge branch 'main' into fork/Eljees/fix/4815-exception-flow-control-…
c8fba2c
…lambda
@UncleOwen
[docs] Update release notes
817461b
@UncleOwen UncleOwen added this to the 7.29.0 milestone Sep 26, 2026
UncleOwen
UncleOwen approved these changes Sep 26, 2026
View reviewed changes

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

This could be considered a new false-negative.

I'll still merge this, because it's definitely an improvement.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@UncleOwen
UncleOwen merged commit 5ce9238 into pmd:main Sep 26, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Sep 27, 2026
[java] ExceptionAsFlowControl: False negative when the lambda is invoked by the method it is passed to #7117
Closed
[java] Fix #7117: ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to #7118
Merged
@Eljees

Eljees commented Sep 27, 2026

Copy link
Copy Markdown
Contributor Author

I looked into the false negative you pointed at. It reproduces on the merged rule: on LeakAwareNettyDataBufferFactory#checkForLeaks the rule reports nothing, because the walk stops at the lambda body. Iterable.forEach performs the action in the calling thread and relays an exception thrown by it to the caller, so that throw is caught by the catch right below it.

Filed #7117 with a fix that keeps the walk going through lambdas passed to the JDK methods whose contract says the action runs in the calling thread; every other lambda stays opaque. #7118 is the PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Eljees added a commit to Eljees/pmd that referenced this pull request Sep 28, 2026
@Eljees
[java] ExceptionAsFlowControl: a throw in a lambda invoked by the met…
4e64d1a
…hod it is passed to

pmd#7105 stopped the walk at every lambda body. That fixed the false positive of
pmd#4815, but it also stopped reporting a throw inside a lambda that the method it
is passed to calls itself. spring-framework's
LeakAwareNettyDataBufferFactory#checkForLeaks (v5.3.13, line 56) throws inside
Iterable.forEach and catches the exception right below.

The walk now continues through a lambda that is an argument of a call whose
method calls that argument: Iterable.forEach, Iterator.forEachRemaining,
Map.forEach, Optional.ifPresent/ifPresentOrElse, and the Stream methods that
take a lambda argument. A terminal operation calls the action while the call is
evaluated; an intermediate operation is evaluated by the terminal operation of
the same pipeline. Every other lambda stays opaque, so pmd#4815 does not come back.

Signed-off-by: Eljees <[email protected]>
@adangel adangel changed the title [java] ExceptionAsFlowControl: stop at a lambda body [java] Fix #4815: ExceptionAsFlowControl: stop at a lambda body Sep 28, 2026
UncleOwen pushed a commit that referenced this pull request Sep 30, 2026
@Eljees
[java] ExceptionAsFlowControl: a throw in a lambda invoked by the met…
5c9271c
…hod it is passed to (#7118)

* [java] ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to

#7105 stopped the walk at every lambda body. That fixed the false positive of
#4815, but it also stopped reporting a throw inside a lambda that the method it
is passed to calls itself. spring-framework's
LeakAwareNettyDataBufferFactory#checkForLeaks (v5.3.13, line 56) throws inside
Iterable.forEach and catches the exception right below.

The walk now continues through a lambda that is an argument of a call whose
method calls that argument: Iterable.forEach, Iterator.forEachRemaining,
Map.forEach, Optional.ifPresent/ifPresentOrElse, and the Stream methods that
take a lambda argument. A terminal operation calls the action while the call is
evaluated; an intermediate operation is evaluated by the terminal operation of
the same pipeline. Every other lambda stays opaque, so #4815 does not come back.

Signed-off-by: Eljees <[email protected]>

* Use compound invocation matcher and clarify release notes

* [java] Handle primitive streams in ExceptionAsFlowControl

---------

Signed-off-by: Eljees <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@UncleOwen UncleOwen UncleOwen approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.29.0

Development

Successfully merging this pull request may close these issues.

[java] ExceptionAsFlowControl: False positive on Lambda/asynchronous

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Eljees @UncleOwen
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.