Lesson 20 of 55
PythonException Handling
20 minutes
Course progress36%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 20 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
Errors in Python raise exceptions. Use try/except blocks to catch and handle them gracefully, optionally providing else and finally clauses.
try:
result = 10 / 0
except ZeroDivisionError as e:
print('Error:', e)
else:
print('Result is', result)
finally:
print('Cleanup (if any)')
The else block runs only if no exception was raised, while finally always runs – perfect for releasing resources.