Skip to content
← Python for AI/ML · all 50 levels

try / except

Level 42 of 50 · Errors, files & modules

Level 42 of 50

try / except

stop your code from crashing. Watch the reel, then work through it below: run the code, take the quiz, keep the cheat sheet.

📒 Open the cheat sheet
SanaI typed the rickshaw fare as "sau", in words, and my app crashed!
TeacherCatch the error before it crashes! try and except!
fare.py
try:
    fare = int("sau")
    print(fare)
except ValueError:
    print("Numbers only!")
TeacherTry the risky line. If it fails, except catches it. No crash!
SanaDoes print(fare) still run?
TeacherNo! The moment an error hits, Python jumps straight to except!
bill.py
bill = 1200
friends = 0
try:
    print(bill / friends)
except ZeroDivisionError:
    print("Nobody came!")
TeacherSplitting the bill with 0 friends? Catch that error too!

⏸ Pause & predict: what prints?

try:
    x = int("5") + "5"
    print(x)
except ValueError:
    print("value")
except TypeError:
    print("type")
Next: raise →← Recursion

Where this leads

Python is step one. Building AI that ships is the rest.

The diploma takes you from zero to deployed computer vision, LLM and agent projects, with engineers who build them for a living. Already code? The bootcamp does it in three months.