Python Intermediate Lesson 14
Python Exceptions
Handle expected failures with try, except, else, finally, and custom raises.
What Is Exception Handling?
An exception is a runtime problem that interrupts normal program flow. Exception handling lets you respond to expected failures without crashing the whole program.
try:
age = int(input("Age: "))
except ValueError:
print("Please enter a number.")
If conversion fails, the except block runs.
Catch Specific Exceptions
Catch the error you expect.
try:
result = 10 / number
except ZeroDivisionError:
print("Cannot divide by zero.")
Specific exceptions make bugs easier to understand. A broad except: can hide problems you did not mean to handle.
The else Block
The else block runs only if no exception happened in the try block.
try:
number = int("42")
except ValueError:
print("Invalid number")
else:
print("Conversion worked")
Use else for code that should run after a successful try.
The finally Block
The finally block runs whether an exception happened or not.
try:
file = open("data.txt")
except FileNotFoundError:
print("Missing file")
finally:
print("Finished attempt")
This is useful for cleanup. For files, a with statement is usually cleaner.
Raising Exceptions
Use raise when your code detects a problem and should report it.
def set_age(age):
if age < 0:
raise ValueError("age cannot be negative")
return age
Raising an exception is better than silently accepting invalid data.
Common Mistakes
A common mistake is catching every exception without understanding it.
try:
run_program()
except:
pass
This hides real bugs. Catch specific exceptions and give helpful responses.
Another mistake is putting too much code inside one try block. Keep the block focused so you know which line is likely to fail.
Quick Summary
- Exceptions are runtime errors.
trycontains code that may fail.excepthandles a specific failure.elseruns only if no exception happened.finallyruns no matter what.raisereports a problem intentionally.- Avoid broad exception handling that hides bugs.
Practice Quiz
Now practice this topic with MCQs and explanations: