Module 7

try / except

Handling errors gracefully.

Catching errors

The try/except block lets you handle exceptions gracefully. You put the risky code in the try block and the fallback in the except block.

Python
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
Cannot divide by zero
Your task

Handle an exception

  • Use try/except to catch ZeroDivisionError.