Exception Arena — Errors & exceptions
Six waves of hostile input. Survive all of them — without hiding a single bug.
How to play
Each level throws a table of inputs at your function, including the ones that break it. Handle them and every
row goes green. What you cannot do is paper over the problem: except: on its own is blocked on the
levels where it would be the lazy answer, because catching everything is how real bugs get hidden for months.
What each level teaches
- Catch it —
try/except, and that a program can carry on from a failure. - Name the exception — catching one specific type and letting everything else fail loudly.
- Two kinds of trouble — one
exceptcan name several types. - Whatever happens —
elseruns when nothing went wrong;finallyruns either way. - Refuse bad input —
raise, so a function rejects what it cannot honour. - Your own exception — a custom class, so callers can catch your failure specifically.
Why a bare except is a trap
Write except: and you catch everything — including the typo you just made, the
MemoryError that means something is badly wrong, and the Ctrl+C you are pressing to stop
the program. The symptom is a program that never crashes and never works: it quietly returns the fallback value
while the real cause stays invisible.
Name the exception you are expecting. If something else goes wrong, you want to be told.
else and finally, briefly
elseruns only if thetryblock raised nothing. It is for the code that should not itself be protected by thetry— keeping the guarded part as small as possible.finallyruns whichever way it went, even if you return from inside thetry. It is for cleanup: closing files, releasing locks, stopping a timer.
Level 4 makes both visible by returning the order things actually happened in.
Ask or apologise?
Python leans towards "easier to ask forgiveness than permission" — try the thing and handle the failure, rather
than checking every precondition first. That is why .get() and try/except KeyError are
both normal here, while in some other languages checking first is the only style. Neither is always right: check
first when the check is cheap and the failure is likely; try first when the check would duplicate the work. The
written version, with the full list of built-in exceptions, is in the tutorial on
Python errors and exceptions, and the matching bugs appear in
15 common Python errors.
Frequently asked questions
What is the difference between an error and an exception in Python?
In everyday use they are the same thing. Strictly, a SyntaxError stops the file being run at all, while an exception is raised while the program is running and can be caught with try/except.
Why should I not use a bare except?
Because it catches everything, including typos (NameError), out-of-memory errors and Ctrl+C. The program then looks like it is working while the real fault is hidden. Name the exception you expect.
What is the difference between except and finally?
except runs only when a matching exception was raised. finally runs either way — including when you return from inside the try — which is why it is used for cleanup.
When should I raise my own exception?
When a function is given something it cannot sensibly handle. Raising immediately is better than returning None or a wrong value, because the failure surfaces at the point where the mistake was made.
How do I create a custom exception in Python?
Subclass Exception: class TooLarge(Exception): pass. That is enough for callers to catch your specific failure without catching everyone else's.
Keep going
- Read the errors & exceptions tutorial — the written explanation behind every level here.
- Variable Vault — variables & data types.
- Operator Arcade — numbers & operators.
- String Surgeon — strings & slicing.
- 20 Python practice exercises with worked solutions.
Take the games with you
Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.
Get it on Google Play