Exception Handling Performance
May 30, 2026 | 5 min read
Do Not Use Exceptions for Flow Control
# Bad
for x in data:
try:
result.append(d[x])
except KeyError:
pass
# Good
get = d.get
for x in data:
val = get(x)
if val is not None:
result.append(val)Exception Handling Compiles Fine
try/except blocks themselves compile well. The overhead is in raising exceptions frequently.