Courses/Errors & Files

⚠️ Errors & Files

try / except

Catch errors instead of crashing.

Wrap risky code in try. Handle specific exception types in except. Use finally for cleanup that always runs.

try:
    x = int("hello")
except ValueError as e:
    print(f"Bad input: {e}")
finally:
    print("done")
main.py
Output
Press Run to execute.
Expected output
5.0
None

Sign in to track your progress across lessons.