Skip to content
Justice Tech School

Errors and try/except

When something goes wrong Python raises an exception. Wrap risky code in try and handle the problem in except so your program does not crash.

Example
try:
    number = int("abc")
except ValueError:
    print("That is not a number")
print("The program keeps running")

Result

That is not a number
The program keeps running

Exercise

Catch the error.

try:
    int("x")
 ValueError:
    print("bad")

Quiz Test

Which block runs when an error happens in try?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.