Skip to content

Cleaner exception handling#1566

Merged
elharo merged 1 commit into
masterfrom
exc
May 16, 2026
Merged

Cleaner exception handling#1566
elharo merged 1 commit into
masterfrom
exc

Conversation

@elharo
Copy link
Copy Markdown
Contributor

@elharo elharo commented Dec 19, 2025

No description provided.

@elharo elharo marked this pull request as ready for review December 19, 2025 13:35
@elharo elharo requested a review from rmannibucau December 19, 2025 13:35
@rmannibucau
Copy link
Copy Markdown
Contributor

rmannibucau commented Dec 19, 2025

@elharo hmm, why is it cleaner ? Difference is not much except your flavor adds bytecode for a case which will never happen so basically you add dead code from my window, any real case worth a unit test and this change?

@elharo
Copy link
Copy Markdown
Contributor Author

elharo commented Dec 19, 2025

  1. It removes a formal variable and keeps everything within the try block. This is how try blocks were designed.
  2. If it turns out that the IOException is not actually impossible, then it's signaled instead of being swallowed so the bug can be addressed.

@rmannibucau
Copy link
Copy Markdown
Contributor

  1. well this is discussable even if I agree on the intent, but the code pattern you do use is also wrong for try blocks cause it misses the flush (in terms of "cleaness", luckily this does nothing there but if you assume that you can also assume there will never be any IOException) which is a well know issue of the return within the try so looks worse than before to me
  2. not a fan of coding for an hypothesis, there is literally not a single line throwing that exception in the related class so it is impossible and other exceptions will blow up as expected

so tempted to think this PR is not cleaner so really +-0 since it will literally just add dead code and not change the behavior

@elharo
Copy link
Copy Markdown
Contributor Author

elharo commented Dec 19, 2025

try-with-resources handles the flush. That's what this block was originally trying to do with the ignored variable.

@rmannibucau
Copy link
Copy Markdown
Contributor

rmannibucau commented Dec 19, 2025

try-with-resources handles the flush.

Please run

class Foo {
    @Test
    void run() throws IOException {
        try (final var writer = new StringWriter() {
            @Override
            public void flush() {
                throw new IllegalStateException("this will not happen");
            }
        }) {
            writer.write("test");
        }
    }
}

That's what this block was originally trying to do with the ignored variable.

No, the current code works cause we call toString after close which does handle flush, no more with your version

@slawekjaranowski slawekjaranowski added the waiting-for-feedback Waiting for 90 days until issues or pull request will be closed label Jan 27, 2026
@github-actions
Copy link
Copy Markdown

This pull request is stale because it has been waiting for feedback for 60 days. Remove the stale label or comment on this PR, or it will be automatically closed in 30 days.

@github-actions github-actions Bot added the Stale label Mar 29, 2026
@elharo elharo removed waiting-for-feedback Waiting for 90 days until issues or pull request will be closed Stale labels Mar 29, 2026
@elharo elharo merged commit ec5a98b into master May 16, 2026
37 checks passed
@elharo elharo deleted the exc branch May 16, 2026 11:24
@github-actions
Copy link
Copy Markdown

@elharo Please assign appropriate label to PR according to the type of change.

@github-actions github-actions Bot added this to the 3.11.0 milestone May 16, 2026
@elharo elharo added the enhancement New feature or request label May 16, 2026
@rmannibucau
Copy link
Copy Markdown
Contributor

hmm, why this broken pattern got merged? we shouldnt encourage to read before closing IO IMHO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants