PLAY FREE ▸
Snekcade › Python guides › Errors and try/except
PYTHON FOR BEGINNERS · BUG BAY

Python errors and try/except

Errors are clues, not failures. Learn to read them and handle them calmly.

6 min read · updated October 2026

Every programmer sees errors, every day. An error is not a failure: it is Python telling you exactly what went wrong and on which line. This guide shows how to read errors, and how to handle them with try and except.

READING AN ERROR MESSAGE

When Python stops with an error, read the last line first. It tells you the type of error and a short reason.

age = int("twelve")
ValueError: invalid literal for int() with base 10: 'twelve'

Translation: int() could not turn the text "twelve" into a number.

ERRORS YOU WILL MEET FIRST

TRY / EXCEPT: CATCH THE ERROR

Put risky code in try. If an error happens, Python jumps to except instead of crashing.

text = "twelve"
try:
    age = int(text)
    print("Next year you will be", age + 1)
except ValueError:
    print("Please type a number, like 12")
Please type a number, like 12
Name the error you expect (except ValueError:). A bare except: also hides typos and real bugs.

ELSE AND FINALLY

for value in ["8", "x"]:
    try:
        n = int(value)
    except ValueError:
        print(value, "is not a number")
    else:
        print(value, "doubled is", n * 2)
    finally:
        print("checked", value)
8 doubled is 16
checked 8
x is not a number
checked x

else runs only when there was no error. finally always runs.

WHAT DOES THIS PRINT?
try:
    print(10 / 0)
except ZeroDivisionError:
    print("oops")
print("done")
Dividing by zero raises ZeroDivisionError, so except prints "oops". The program then carries on and prints "done".

DEBUGGING TIPS

COMMON BEGINNER MISTAKES

Hiding every error with except: pass. The program "works" but silently does the wrong thing.
Putting too much code inside try. Keep only the risky line or two there.
WHAT DOES THIS PRINT?
data = {"hp": 5}
try:
    print(data["mana"])
except KeyError:
    print("no mana")
except ValueError:
    print("bad value")
The key "mana" is missing, which raises KeyError. Only the first matching except runs.

PRACTICE IT, DON'T JUST READ IT

Bug Bay in Snekcade is all about debugging: squash 10 bugs in a broken game, with hints that get more specific each time you ask.

PLAY BUG BAY ▸

FAQ

How do I read a Python error message?

Start at the last line: it names the error type and gives a short reason. The lines above show which line of your code caused it.

What is the difference between SyntaxError and other errors?

A SyntaxError means the code is not valid Python, so it does not run at all. Other errors happen while the code is running.

Should I use a bare except?

Usually not. Catch the specific error you expect, like except ValueError, so real bugs are not hidden.