Code in 'try' runs normally; if it raises an exception, Python looks for the FIRST 'except' clause matching that exception's type (checked top to bottom) and runs it — if none match, the exception propagates up the call stack, potentially crashing the program if never caught anywhere.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid value")
Real-world example
Gracefully handling a division-by-zero or invalid-input error instead of letting the program crash with an unhandled traceback.
Likely follow-ups
What happens if you put a more GENERAL exception type's except clause BEFORE a more SPECIFIC one?
Code in 'finally' ALWAYS runs after try (and any except) completes, whether an exception occurred or not, and even if the try block contains a 'return' — making it the standard place for cleanup code like closing a file or releasing a lock.
file = open("data.txt")
try:
process(file)
finally:
file.close() # always runs, guaranteeing the file is closed
Real-world example
Guaranteeing a file handle or network connection is properly closed regardless of whether processing succeeded or raised.
Likely follow-ups
How does the 'with' statement relate to and often replace a manual try/finally for resource cleanup?
List the exception types as a TUPLE after 'except', separated by commas — the clause catches ANY of the listed types, running the same handling code regardless of which specific one was raised.
try:
value = int(user_input)
result = 10 / value
except (ValueError, ZeroDivisionError) as e:
print(f"Invalid input: {e}")
Real-world example
Handling several related error conditions (like invalid input format AND division by zero) with the same recovery logic.
Likely follow-ups
How would you handle these SAME exception types differently, with SEPARATE recovery logic for each?
Define a class inheriting from Exception (or a more specific built-in exception), typically with no additional code needed at all (just 'pass') unless you want to add custom attributes or override __init__ — Python's exception machinery handles the rest automatically via inheritance.
class InsufficientFundsError(Exception):
def __init__(self, shortfall):
super().__init__(f"Insufficient funds. Short by ${shortfall:.2f}")
self.shortfall = shortfall
try:
raise InsufficientFundsError(50.00)
except InsufficientFundsError as e:
print(e.shortfall) # 50.0
Real-world example
Raising a domain-specific exception (like InsufficientFundsError) that carries meaningful business context beyond a generic message.
Likely follow-ups
Why is it good practice to inherit custom exceptions from a specific built-in type rather than always using the bare 'Exception'?
Plain 'raise NewException()' inside an except block still shows BOTH the original and new exception in the traceback (implicit chaining, labeled 'During handling of the above exception...'); 'raise NewException() from original_exception' makes the chaining EXPLICIT, and 'raise NewException() from None' SUPPRESSES the original exception's context entirely, showing only the new one.
try:
int("not a number")
except ValueError as e:
raise RuntimeError("Failed to parse config") from e # explicit chaining: shows both, labeled 'direct cause'
try:
int("not a number")
except ValueError:
raise RuntimeError("Failed to parse config") from None # suppresses the ValueError entirely
Real-world example
Wrapping a low-level parsing error in a more meaningful, higher-level exception while preserving (or intentionally hiding) the original cause.
Likely follow-ups
When would you deliberately want to suppress the original exception's context using 'from None'?
sys.exc_info() returns a 3-tuple (exception type, exception instance, traceback object) for the exception CURRENTLY being handled — mostly superseded by simply using 'except Exception as e' to directly capture the instance, but still useful for lower-level introspection or re-raising logic.
The 'else' clause runs ONLY if the 'try' block completed WITHOUT raising any exception — putting code there (instead of at the end of 'try') makes it explicit that this code is NOT meant to be protected by the except clauses, avoiding accidentally catching exceptions from code that wasn't the original risky operation.
try:
value = risky_parse(data)
except ValueError:
print("Parsing failed")
else:
print(f"Parsed successfully: {value}") # only runs if risky_parse succeeded, and its own errors aren't caught above
Real-world example
Clearly separating the 'risky' operation being protected from subsequent code that shouldn't accidentally have its own errors caught by the same except clause.
Likely follow-ups
What would go wrong if the 'else' block's code were instead placed at the END of the 'try' block?
ExceptionGroup wraps multiple exceptions raised together (common in concurrent code, like asyncio.gather with multiple failures) as a single object; 'except*' matches and handles the SUBSET of exceptions within the group matching a given type, potentially re-raising a NEW ExceptionGroup with the remaining unmatched ones.
Handling multiple independent failures from concurrent tasks (like several parallel API calls) that fail simultaneously with different exception types.
Likely follow-ups
How does asyncio.gather() (or TaskGroup in 3.11+) actually produce an ExceptionGroup from multiple failed concurrent tasks?
Create a base application exception (e.g., AppError), then specific subclasses for different error categories (e.g., ValidationError, NotFoundError, all inheriting from AppError) — callers can catch broadly (`except AppError`) to handle ALL application errors generically, or narrowly (`except ValidationError`) for specific recovery logic, without needing to know every specific subclass upfront.
class AppError(Exception):
pass
class ValidationError(AppError):
pass
class NotFoundError(AppError):
pass
try:
validate_and_fetch(data)
except ValidationError:
return {"error": "invalid input"}, 400
except AppError:
return {"error": "application error"}, 500 # catches NotFoundError and any FUTURE AppError subclass too
Real-world example
Designing a web API's error-handling layer so new exception types added later are automatically caught by broader existing handlers.
Likely follow-ups
Why is designing a THOUGHTFUL exception hierarchy upfront valuable for a growing codebase's error handling?
__exit__(self, exc_type, exc_value, traceback) receives details of any exception raised inside the 'with' block; if it returns a TRUTHY value, the exception is SUPPRESSED entirely (treated as handled, execution continues after the 'with' block) — returning None/False (the default) lets the exception propagate normally.
class SuppressErrors:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, tb):
if exc_type is ValueError:
print(f"Suppressed: {exc_value}")
return True # suppresses the ValueError
return False # lets other exception types propagate normally
with SuppressErrors():
raise ValueError("this gets suppressed")
print("Execution continues here")
Real-world example
Building a custom context manager (similar to contextlib.suppress) that intentionally swallows specific, expected exception types.
Likely follow-ups
How does the standard library's contextlib.suppress() context manager provide this exact same capability more concisely?