← All posts
postMay 30, 2026
Python try/except — handle errors before they crash your pipeline
#python#error-handling#best-practices
python
rows = ["100", "200", "abc", "300"]
total = 0
errors = 0
for r in rows:
try:
total += int(r)
except ValueError:
errors += 1
print(f"sum={total} errors={errors}")
# sum=600 errors=1Real-world data sources fail in mundane ways: a row has a missing field, a number is unparseable, an API times out. Without error handling, one bad row crashes the entire pipeline. With try/except you catch the failure, log it, and keep processing.
The pattern is simple: wrap the risky operation in try, catch the specific exception you expect, decide whether to skip, retry, or alert. Catch specific exceptions like ValueError or KeyError — never use a bare except, because it hides bugs you actually need to see.