Starlit Daily

📚 › Python: the friendliest first language

13. Handling errors with try and except

try runs code that might fail, and except handles the error. Without it, a single mistake stops the whole program.

Read the error message: it names the problem and the line.

Show example

try:
    print(10 / 0)
except ZeroDivisionError:
    print("Cannot divide by zero")

✍️ Your turn

Use try and except: in try divide 1 by 0, in except print Cannot divide.

← Back to course