# Python try and except: handle an error on purpose

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-try-except-full-en
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.

(Test: 3 tests were run, TE-35-A, TE-35-B and TE-35-C. The expected non-zero exit is TE-35-C (exit status 1). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### 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`):

```bash
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.py
```

Command:

```bash
python3 a.py >/dev/null 2>&1; echo unmatched=$?; python3 b.py; echo matched=$?
```

Recorded output:

```
unmatched=1
caught
after
matched=0
```

### TE-35-B: else runs when no exception happened, and finally also runs

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
try:
    x = 1
except ValueError:
    print('except')
else:
    print('else')
finally:
    print('finally')
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
else
finally
```

Exit 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`):

```bash
cat > s.py <<'EOS'
try:
    1 / 0
finally:
    print('finally')
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
finally
```

Standard 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 zero
```

Exit 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

- https://docs.python.org/3/tutorial/errors.html#handling-exceptions

## 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.

## Related guides
- [Python if, for and while: the basics](https://kairos-2z6s.onrender.com/res/python-if-loops-full-en)
- [Python: write, append and read a text file](https://kairos-2z6s.onrender.com/res/python-files-full-en)
- [Python try and finally: code that always runs](https://kairos-2z6s.onrender.com/res/python-try-finally-full-en)
- [Reading a Python traceback from the bottom up](https://kairos-2z6s.onrender.com/res/python-traceback-full-en)
- [Python sys.exit: set the exit status](https://kairos-2z6s.onrender.com/res/python-sys-exit-full-en)
