Skip to content

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

Open
toddr-bot wants to merge 1 commit into
mainfrom
koan.toddr.bot/fix-empty-catch
Open

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

Conversation

@toddr-bot

@toddr-bot toddr-bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

What

Empty CATCH blocks (TRY; ...; CATCH; END) now correctly catch and suppress exceptions.

Why

Directive::try() used if ($default) to check whether a default CATCH handler existed.
An empty CATCH body compiles to an empty string, which is falsy in Perl — so the handler
was silently discarded and exceptions propagated as if no CATCH was present.

How

Changed if ($default) to if (defined $default) in Directive.pm:672. This distinguishes
"no CATCH specified" (undef) from "empty CATCH body" (''). The generated code already
includes $_tt_error = '' in the default handler block, which correctly clears the error.

Testing

  • Added 4 test cases to t/try.t: empty CATCH, empty CATCH with FINAL, empty typed CATCH
    with default fallthrough, and empty CATCH with multiline TRY body.
  • Full test suite passes.

Fixes #271


Quality Report

Changes: 2 files changed, 31 insertions(+), 1 deletion(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan

@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.

An empty CATCH block (e.g., TRY; ...; CATCH; END) was silently failing
to catch exceptions because Directive::try() used 'if ($default)' to
check for a default handler. An empty block produces an empty string,
which is falsy in Perl, so it was treated as 'no handler' and the
exception propagated.

Fix: change to 'if (defined $default)' to distinguish between 'no
CATCH specified' (undef) and 'empty CATCH body' (empty string).
@toddr-bot
toddr-bot force-pushed the koan.toddr.bot/fix-empty-catch branch from 9b278a2 to fef0e07 Compare July 2, 2026 23:16
@toddr-bot toddr-bot changed the title fix: empty CATCH block now catches errors (GH #271) fix: empty CATCH block now catches exceptions (GH #271) Jul 2, 2026
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