Home βΊ Problems βΊ Wrong exception type caught
Wrong exception type caught
π Fix the bug Topic: exceptions Difficulty: medium Free
What this problem practises
Exceptions are control flow. except clauses are matched top to bottom, finally runs even after a return, and catching too broadly hides the real bug behind a generic message.
This is a fix-the-bug problem: a short program is given in its broken state, and the task is to repair the code so it produces the required output. The interactive editor runs your fixed version in the browser and checks it for you β no setup, no local interpreter.
Difficulty: medium β expect to hold two interacting rules in your head at once, such as a boundary combined with a mutation.
The challenge
This program should print "error" but it still crashes. Fix the except clause so the crash is caught and it prints error.
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The error it raises
Run the broken snippet and Python stops with this exact error: Uncaught exception bypassing mismatched except handler. Match it to the Python errors guide, then repair the line that produced it β knowing the message is half the fix.
The snippet
The interactive editor pre-fills exactly this code, broken and ready to repair:
try:
print(46 / 0)
except ValueError:
print('error')Understand the rule
An `except SomeError:` clause catches ONLY instances of `SomeError` (and its subclasses). If the `try` block actually raises `ZeroDivisionError` or `KeyError` while the handler listens for `ValueError`, the exception sails right past `except` and crashes the program.
How to trace it
- Run the `try` block on paper and identify the exact built-in exception class the failing operation raises.
- Python checks `isinstance(raised_exc, CaughtType)` against the `except` header.
- When `CaughtType` does not match (for example, catching `IndexError` on a dict lookup that raises `KeyError`), the handler is skipped.
- Updating `except` to the true exception class (or a tuple like `except (KeyError, IndexError):`) lets the fallback run cleanly.
Common wrong fixes
- Catching `IndexError` on a dictionary lookup (raises `KeyError`) or `KeyError` on a list index (raises `IndexError`).
- Writing `except KeyError, ValueError:` (invalid Python 2 syntax β Python 3 requires parentheses `except (KeyError, ValueError):`).
- Using a bare `except:` everywhere, which dangerously swallows `KeyboardInterrupt` and typos like `NameError` inside the `try` block.
Rule to remember
Match the `except` clause to the exact exception raised (`ZeroDivisionError`, `KeyError`, `IndexError`, `ValueError`, `TypeError`) rather than guessing.
More exceptions problems
Keep going
- Python debugging guide β a systematic method for finding bugs like this one fast.
- Browse every exceptions problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.