Class TryCatchAsmGenerator


final class TryCatchAsmGenerator extends AbstractControlFlowAsmGenerator
Specialized generator for EK9 try/catch/finally statements. Handles CONTROL_FLOW_CHAIN with chain_type: "TRY_CATCH_FINALLY".

JVM Bytecode Pattern for try/catch:

try_start:
  [try block instructions]
  goto after_handlers            ; Normal exit
try_end:
catch_start_1:
  [catch block 1 instructions]
  goto after_handlers
catch_end_1:
after_handlers:
  [continue execution]

JVM Bytecode Pattern for try/finally (unified finally block):

try_start:
  [try block instructions]
try_end:
  goto finally_block            ; Normal path
catch_start:
  [catch block instructions]
  goto finally_block            ; Catch path
finally_exception:
  astore temp_exception         ; Store exception
  goto finally_block            ; Exception path
finally_block:
  [finally block - executes once for all paths]
  aload temp_exception (if set)
  ifnull after_finally
  athrow                        ; Rethrow if exception present
after_finally:
  [continue execution]

Exception Table:
  from    to      target          type
  try_start try_end finally_exception null (catch-all)

Stack Frame Invariant: Stack is empty before try, after each handler, and after the entire statement.

  • Constructor Details

  • Method Details

    • generate

      public void generate(ControlFlowChainInstr instr)
      Generate bytecode for try/catch/finally.

      Control Flow:

      1. Create labels for exception handlers and finally blocks
      2. Register exception handlers with JVM (visitTryCatchBlock)
      3. Execute try block
      4. Execute finally block (normal path) if present
      5. Jump to after handlers
      6. Execute each catch handler
      7. Execute finally block (exception path) if present

      Stack Frame Invariant: Pre-condition: stack is empty Post-condition: stack is empty

      Parameters:
      instr - CONTROL_FLOW_CHAIN instruction with TRY_CATCH_FINALLY type