try / except / else / finally
Structure exception-handling blocks for any scenario.
Introduction
Python's try/except/else/finally gives you complete control over error handling and cleanup.
Basic try/except
try: risky_code() except ExceptionType: handle() — Python executes the except block only when that exception type is raised.
try:
x = int('abc')
except ValueError:
print('not a number')All lessons in this course
- Understanding Exceptions
- try / except / else / finally
- Raising and Re-raising Exceptions
- Custom Exception Classes