try/finally does not behave as expected when rethrowing · Issue #1137 · TypeScriptToLua/TypeScriptToLua · GitHub
Skip to content

try/finally does not behave as expected when rethrowing #1137

Closed

Description

@Perryvw

See re-throw test in test/unit/error.spec.ts:

const i: number = ${i};
function foo() {
    try {
        try {
            if (i === 0) { throw "z"; }
        } catch (e) {
            throw "a";
        } finally {
            if (i === 1) { throw "b"; }
        }
    } catch (e) {
        throw (e as string).toUpperCase();
    } finally {
        throw "C";
    }
}
let result: string = "x";
try {
    foo();
} catch (e) {
    result = (e as string)[(e as string).length - 1];
}
return result;

Activity

  1. hepiyellow commented on Jun 7, 2023

    @hepiyellow

    Just to clarify and simplify.
    The issue is that the Finally block is not getting called.

    This TypeScript code when compiled with tsc:

    try {
        try {
            throw 'the error'
        } catch (err) {
            console.log('Catch Block')
            throw 'rethrow'
        } finally {
            console.log('Finally Block')
        }
    } catch (err) {
        console.log(`Outer Catch`)
    }

    Produces this output:

    Catch Block
    Finally Block
    Outer Catch
    

    The equivalent code in TSTL

    try {
            try {
                error('the error')
            } catch (err) {
                Echo('Catch Block')
                error(`rethrown`)
            } finally {
                Echo('Finally Block')
            }
        } catch (err) {
            Echo(`Outer Catch`)
    }

    Produces this output:

    Catch Block
    Outer Catch
    

    The finally block is NOT called 😞

  2. thautwarm commented on Sep 14, 2023

    @thautwarm

    bump

  3. thautwarm commented on Sep 14, 2023

    @thautwarm

    throw not working in the try block when finally is presented.

  4. thautwarm commented on Sep 14, 2023

    @thautwarm
    try
    {
        print(1);
        throw new Error("123");
    }
    finally
    {
        print(2);
    }
    
    print(3);

    Above code smoothly gives me

    1
    2
    3
    
  5. hepiyellow commented on Sep 14, 2023

    @hepiyellow

    @thautwarm The above code you show seems to be doing what is expected.
    The problem starts when you re-throw an error in the catch block.

    try {
            try {
                error('the error')
            } catch (err) {
                Echo('Catch Block')
                error(`rethrown`)
            } finally {
                Echo('Finally Block')
            }
        } catch (err) {
            Echo(`Outer Catch`)
    }

    The output is:

    Catch Block
    Outer Catch
    

    The finally block is not called 😞

  6. thautwarm commented on Sep 17, 2023

    @thautwarm

    I apologize for this. I didn't recognize this issue is not related to my question.

  7. added a commit that references this issue on Jun 30, 2026
    b248e41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions