WHILE, BREAK, and TRY/EXCEPT
Chapter lab: Variables and control flow
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:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch07-control-flow.zip && unzip -q ch07-control-flow.zip && rm ch07-control-flow.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch07-control-flow.zip -OutFile ch07-control-flow.zip; Expand-Archive ch07-control-flow.zip -DestinationPath .; Remove-Item ch07-control-flow.zipOpen 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:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 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:
WHILErepeats while a condition holds.BREAKexits a loop early;CONTINUEskips to the next iteration.TRY/EXCEPTcatches a failing keyword and handles it, instead of failing the test. OptionalELSEandFINALLYblocks 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”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 ENDDone 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 ENDTask 2 — Run the suite green
Section titled “Task 2 — Run the suite green”Run it:
uv run robot checksrobot checkspoetry run robot checksDone 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.
BREAKinside a loop…BREAKexits the loop;CONTINUEskips to the next iteration.Show the answer
Answer: exits the loop immediately —
BREAKexits the loop;CONTINUEskips to the next iteration.TRY/EXCEPT lets a test…
TRY/EXCEPT catches a failure from the TRY block and runs the matching EXCEPT handler instead of failing the 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.