Skip to content

Fix try/catch inside a catch clause whose try has a finally - #349

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-catch-in-catch-finally
Sep 23, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-catch-in-catch-finally

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

A try/catch written inside a catch clause crashed when the outer try also had a finally. It crashed even when the nested catch matched, for example:
try { throw 1 } catch (e: int) { try { throw 2 } catch (x: int) {} } finally {}.
Without the finally this already worked (00nested_catch.ts).

Cause (Windows). With a finally, a throw inside a catch clause deliberately stays inside the catch funclet: it invokes into the finally, and the finally is what ends the catch. A throw inside a nested try is different. It unwinds to the nested try's own pad, which is emitted at the top level (catchswitch within none). So an invoke inside a catchpad unwound to a pad outside it, which is invalid funclet nesting.

Fix. A throw inside a nested try now ends the outer catch first, exactly as it already did when there is no finally.

Also fixed. An exception the nested catch doesn't take skipped the outer finally. A try in its parent's catch clause is not the parent's to catch, but the parent's finally must still run. Its mismatch edge now chains to the parent's finally, using the new side tables finallyBlockOf and inParentTryCatch. This applies on Windows and on the Itanium (Linux) path.

Test. New 00try_in_catch_with_finally.ts covers:

  • a nested catch that matches;
  • a nested mismatch reaching the caller after the outer finally;
  • two nested finally blocks running innermost first;
  • a throw after the nested try.

Verified locally.

  • Windows ctest: 2818/2818.
  • Linux (WSL) ctest: 2812/2812.
  • check-x86-eh.sh: 53/53.
  • The new test also passes as an i686 program.

🤖 Generated with Claude Code

With a finally, throws in a catch clause stay inside the catch funclet (they
invoke into the finally, which ends the catch). A throw inside a try nested in
that catch unwinds to the nested try's own pad instead - a top-level pad reached
from inside a catchpad, invalid funclet nesting - so even a matching nested
catch crashed at run time. Such a throw now ends the outer catch first, as it
already did without a finally.

An exception the nested catch does not take also skipped the outer finally. A
try in its parent's catch clause now chains its mismatch to the parent's
finally (new finallyBlockOf / inParentTryCatch side tables), on Windows and on
the Itanium path alike.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit f970734 into main Sep 23, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-catch-in-catch-finally branch September 23, 2026 14:00
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.

1 participant