Exceptions signal contract violations cleanly
Beginner
Defensive APIs: validation, sanitization & exceptions
Created by Pavel
· 29.04.2026 at 19:10 UTC
Return codes like -1 or sentinel strings collide with legitimate data: a temperature of -1 might be real, and the string "none" might be a valid category label. Exceptions separate happy-path returns from failure signalling and carry stack context when uncaught.
In data pipelines, parse JSON, CSV, and query parameters at boundaries; raise ValueError or domain-specific errors when contracts break. Catch narrowly at integration points (try / except ValueError) instead of bare except: which also traps KeyboardInterrupt and SystemExit in Python 2 style habits.
Libraries like Pydantic turn validation failures into structured ValidationError objects—same idea: fail loudly with diagnostic information.
Errors and exceptions tutorial: [1].
Sources
University approvals: 0
Tasks
Card Info
- Topic: Defensive APIs: validation, sanitization & exceptions
- Difficulty: Beginner
- Completed: 0 users
Creator
Pavel