Exception Handling¶
Exceptions are errors that occur during program execution.
Python provides try, except, else, and finally to handle errors gracefully.
Basic try-except¶
try:
result = 10 / 0
except:
print("An error occurred")
Catch Specific Exceptions¶
It is better to catch specific errors.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
Multiple Exceptions¶
try:
value = int("abc")
except ZeroDivisionError:
print("Division error")
except ValueError:
print("Invalid value")
else Block¶
Runs only if no exception occurs.
try:
result = 10 / 2
except ZeroDivisionError:
print("Error")
else:
print("Success:", result)
Output¶
Success: 5.0
finally Block¶
Always runs, whether there is an error or not.
try:
file = open("test.txt", "r")
content = file.read()
except FileNotFoundError:
print("File not found")
finally:
print("Execution finished")
Raising Exceptions¶
You can manually raise errors using raise.
age = -1
if age < 0:
raise ValueError("Age cannot be negative")
Custom Exception Handling Pattern¶
def divide(a, b):
if b == 0:
raise ValueError("b cannot be zero")
return a / b
try:
print(divide(10, 0))
except ValueError as e:
print("Error:", e)
Common Built-in Exceptions¶
| Exception | Description |
|---|---|
| ValueError | Invalid value |
| TypeError | Wrong type |
| ZeroDivisionError | Division by zero |
| FileNotFoundError | Missing file |
| IndexError | Invalid list index |
Summary¶
- Use
tryto wrap risky code - Use
exceptto handle errors - Use
elsefor success flow - Use
finallyfor cleanup - Use
raiseto trigger exceptions manually