Skip to content

fix: empty CATCH block now catches errors (GH #271)#417

Open
toddr-bot wants to merge 2 commits into
mainfrom
koan.toddr.bot/fix-empty-catch
Open

fix: empty CATCH block now catches errors (GH #271)#417
toddr-bot wants to merge 2 commits into
mainfrom
koan.toddr.bot/fix-empty-catch

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

What

Empty CATCH blocks (no body) now properly catch and suppress errors.

Why

A TRY; THROW ...; CATCH; END construct should silently catch the error, but instead the error propagated as if no CATCH existed. This was reported in #271.

How

The bug was in Directive::try() — the if ($default) truthiness check treated an empty block (empty string '') the same as no block at all (undef). Changed to if (defined $default) to distinguish between "catch with empty body" and "no catch at all". Also hardened the $default assignment to use a defined check, consistent with Perl 5.8 compatibility.

Testing

  • Added 4 test cases to t/try.t covering empty default CATCH, empty named CATCH, mixed named+empty default, and empty catch-all after unmatched named handler.
  • First commit contains the failing tests, second commit contains the fix.
  • Full test suite passes (2900 tests across 112 files).

Closes #271

🤖 Generated with Claude Code


Quality Report

Changes: 2 files changed, 45 insertions(+), 2 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

toddr-bot and others added 2 commits March 23, 2026 22:02
An empty CATCH block (no body) should still catch errors and suppress
them, but currently the error propagates as if no CATCH existed.

These tests will pass once the bug is fixed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
An empty CATCH block like `TRY; ...; CATCH; END` was not catching
errors because the empty block produced an empty string, which is
falsy in Perl. The `if ($default)` check in Directive::try() treated
it as "no default handler", letting the error propagate.

Fix: use `defined($default)` to distinguish an empty catch block
(empty string) from no catch block at all (undef).

Also changed the `$default ||=` assignment to use `defined` check,
ensuring an empty default CATCH isn't overridden by a later one.

Closes #271

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@toddr-bot
Copy link
Copy Markdown
Contributor Author

Recreated from #347 (auto-closed when the toddr-bot fork was removed). Original branch is now hosted on this repo.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty CATCH doesn't catch

1 participant