An exception is an object or signal created when something goes wrong while a program runs, such as a missing file, invalid input or a failed network call. It interrupts normal execution and travels up the call chain until some code catches it. If nothing does, the program crashes and prints a stack trace.
Why it matters
Exceptions separate the error-handling path from the main logic. Handled well, they let you retry, show a friendly message or log details and continue. Handled badly, they hide problems or crash a service, producing 500 errors for visitors.
Example
try:
price = float(user_input)
except ValueError:
price = 0.0
log.warning("Invalid price: %r", user_input)
If user_input is "abc", float() raises ValueError; the except block catches it and the program carries on with a safe default.
Common mistakes
- Catching every exception with an empty handler, which silently swallows real bugs.
- Catching an error and returning nothing useful, so the cause is lost; log it with its trace.
- Using exceptions for normal control flow in hot code paths.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.