Coding Python
Home›Python Coding Games›Exception Arena

Exception Arena — Errors & exceptions

Six waves of hostile input. Survive all of them — without hiding a single bug.

6 levels · Intermediate
Level 1

Checks
    Your PythonPython 3
    1

    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

    1. Catch it — try/except, and that a program can carry on from a failure.
    2. Name the exception — catching one specific type and letting everything else fail loudly.
    3. Two kinds of trouble — one except can name several types.
    4. Whatever happens — else runs when nothing went wrong; finally runs either way.
    5. Refuse bad input — raise, so a function rejects what it cannot honour.
    6. 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

    • else runs only if the try block raised nothing. It is for the code that should not itself be protected by the try — keeping the guarded part as small as possible.
    • finally runs whichever way it went, even if you return from inside the try. 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.

    Practise errors & exceptions on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    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

    ← All Python coding games  Read the tutorial →

    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
    Coding Python app icon