Python try and except: handle an error on purpose
A matching except handles an exception and lets execution continue; an unmatched one propagates. An error inside the handler can still end the program.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-35-A: only a matching except block lets execution continue; an unmatched exception still propagates
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'try:\n 1/0\nexcept ValueError:\n print("caught")\nprint("after")\n' > a.py; printf 'try:\n 1/0\nexcept ZeroDivisionError:\n print("caught")\nprint("after")\n' > b.pyCommand:
python3 a.py >/dev/null 2>&1; echo unmatched=$?; python3 b.py; echo matched=$?Recorded output:
unmatched=1
caught
after
matched=0TE-35-B: else runs when no exception happened, and finally also runs
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
try:
x = 1
except ValueError:
print('except')
else:
print('else')
finally:
print('finally')
EOSCommand:
python3 s.pyRecorded output:
else
finallyExit status: 0. This status and the output match what was written before the run.
TE-35-C: expected non-zero exit: finally still runs when the exception is not handled, and then the exception ends the program
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
try:
1 / 0
finally:
print('finally')
EOSCommand:
python3 s.pyRecorded output:
finallyStandard error from the same run:
Traceback (most recent call last):
File "/tmp/b14-57qovbwu/work/s.py", line 2, in <module>
1 / 0
ZeroDivisionError: division by zeroExit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.
TE-35-B: no exception happened, so else printed else and finally printed finally. The except block did not run. TE-35-C: the exception was not handled. finally printed finally, and then the program ended with exit status 1 and ZeroDivisionError: division by zero on standard error.
Limits
An error inside the handler can still end the program.
Source
Evidence
TE-35-A: only a matching except block lets execution continue; an unmatched exception still propagates
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-35-A | only a matching except block lets execution continue; an unmatched exception still propagates | 0 | PASS | 18cee16a33cd | | TE-35-B | else runs when no exception happened, and finally also runs | 0 | PASS | 39f9339eb9d7 | | TE-35-C | expected non-zero exit: finally still runs when the exception is not handled, and then the exception ends the program | 1 | PASS | 9a10c481afb6 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides