best-practices
Exception Handling Performance
How try/except affects compilation and when to use it.
Published May 30, 2026
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.