Skip to content

TRY/EXCEPT not working in Teardown #4422

Closed

Description

@DirkRichter

Failing keywords in teardown are not handled with TRY/EXCEPT:

Sample robot file:

*** Settings ***
Test Teardown  TeardownKW

*** Keywords ***
TeardownKW
    TRY
        Fail  1
        Fail  2
        RETURN
    EXCEPT
        Log  err handled
    END

*** Test Cases ***
DraftTest
    Log  This test is used for experiments.

Results in:
grafik

Expected: Keywords Fail 2 and RETURN should not be executed due to Fail 1 is failing.

Tested in RF version 5.0 and 5.1a2

Activity

  1. pekkaklarck commented on Aug 9, 2022

    @pekkaklarck
    Member

    Failures don't stop execution in teardown because we want all cleanup activities to be done. That means TRY/EXCEPT structures work differently in teardowns than elsewhere and this was already reported as #4293. As you can see in the comments of that issue, we decided not to change this because teardown behavior changing inside TRY/EXCEPT would have been strange and also complicated to implement. We instead decided to make it possible to explicitly disable the continue-on-failure mode that's automatically on in teardowns. For more details about that see #4303.

  2. alainsiroishw commented on Dec 6, 2025

    @alainsiroishw

    Hello @pekkaklarck

    Would you consider the below best practice: prioritzing robot:recursive-stop-on-failure over robot:stop-on-failure in teardown?

    We could add this to the Robot Framework User Guide.

    *** Settings ***
    Documentation    I am MyResource.resource
    
    
    *** Keywords ***
    Common Complex Operation Used In Many Tests
        # [Tags]    robot:stop-on-failure
        # We want to avoid putting the above tag in many kewords
        TRY
            Log    I am an operation that succeeds or fails 50/50 of the time
            RETURN    ${True}
        EXCEPT
            RETURN    ${False}
        END
    *** Settings ***
    Documentation    I am a Robot file with a test
    Resource    MyResource.resource
    Test Teardown    Teardown After Every Test
    
    
    *** Test Cases ***
    Test One
        Log    I am checking something in Test One
    
    
    *** Keywords ***
    I Am Required In Teardown
        [Tags]    robot:recursive-stop-on-failure
        # Best way is to put this recursive tag here, where the behavior is required.
        Common Complex Operation Used In Many Tests
    
    I Am Also Required In Teardown Please
        Log    Doing some teardown stuff
    
    Teardown After Every Test
        I Am Required In Teardown
        I Am Also Required In Teardown Please
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions