Skip to content

WHILE, BREAK, and TRY/EXCEPT

Chapter lab: Variables and control flowch07-control-flow.zip · 3 KB

This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.

Download ch07-control-flow.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch07-control-flow.zip && unzip -q ch07-control-flow.zip && rm ch07-control-flow.zip

Open the folder in VS Code with code rfe-ch07-control-flow (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 3.13 or newer is required; uv installs it when it is missing.

Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.

What's inside
  • Directoryrfe-ch07-control-flow/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • Directorycatalog/
        • catalog.robot
      • Directorysmoke/
        • smoke.robot
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

Two more native structures round out control flow:

  • WHILE repeats while a condition holds. BREAK exits a loop early; CONTINUE skips to the next iteration.
  • TRY / EXCEPT catches a failing keyword and handles it, instead of failing the test. Optional ELSE and FINALLY blocks round it out.

Both close with END.

Task 1 — Write a WHILE loop with BREAK and a TRY/EXCEPT

Section titled “Task 1 — Write a WHILE loop with BREAK and a TRY/EXCEPT”
Edit

Goal: create checks/robust.robot with a WHILE loop that stops early with BREAK, and a test that catches a failing keyword with TRY / EXCEPT.

Here is the same pair over different numbers and messages. Adapt them.

Worked example — adapt it, don’t copy it

*** Test Cases ***
Counts To Five
${n}= Set Variable ${0}
WHILE ${n} < 100
${n}= Evaluate ${n} + 1
IF ${n} == 5 BREAK
END
Should Be Equal As Integers ${n} 5
Recovers From An Error
TRY
Fail kaboom
EXCEPT kaboom
Log recovered
END

Done when

checks/robust.robot uses WHILE and TRY, and While With Break passes.

Hint 1

Start a counter at ${0}, loop with WHILE ${i} < 10, increment inside with Evaluate, and IF ${i} == 3 BREAK. Close with END.

Hint 2

Wrap a failing keyword: TRY, then Fail boom, then EXCEPT boom, then Log handled, then END.

Solution
*** Test Cases ***
While With Break
${i}= Set Variable ${0}
WHILE ${i} < 10
${i}= Evaluate ${i} + 1
IF ${i} == 3 BREAK
END
Should Be Equal As Integers ${i} 3
Try Except Handles Failure
TRY
Fail boom
EXCEPT boom
Log handled
END
Run

Run it:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

Done when

the test While With Break passes.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. BREAK inside a loop…

    Show the answer

    Answer: exits the loop immediately — BREAK exits the loop; CONTINUE skips to the next iteration.

  2. TRY/EXCEPT lets a test…

    Show the answer

    Answer: catch a failing keyword and handle it — TRY/EXCEPT catches a failure from the TRY block and runs the matching EXCEPT handler instead of failing the test.

Key terms

WHILE / BREAK / CONTINUEconcept
Condition-driven loop with early exit (BREAK) and skip (CONTINUE); close with END.
TRY / EXCEPTconcept
Catch and handle a failing keyword; optional ELSE and FINALLY. Close with END.

All terms in the glossary →