Error handling · 10 sample answers

Python exceptions: practice questions with answers.

Exception handling is not about hiding errors. It is about deciding which errors you can recover from, which ones should stop the program, and what the caller needs to know. These questions test that judgement.

39 questions in the full bank · 14 choice · 14 fill · 11 output

What this topic tests

  • Catch the specific exception you expect, not every error.
  • Trace try, except, else, and finally in order.
  • Raise a clear exception instead of returning a misleading value.

Common mistakes

  • Using a bare except and swallowing programming mistakes.
  • Putting too much code inside the try block, including code that cannot fail in the expected way.
  • Assuming finally runs only when an exception occurs.
Sample questions

Sample questions

10

Ten representative questions from the full bank. Try each one before opening the answer.

  1. Multiple choiceq0972

    1. Ming must close the shop whether or not the milk tea succeeds. When does finally run in try-except-finally?

    1. only on an exception
    2. only when there is no exception
    3. always, whether or not there is an exception
    4. never
    Show answer

    Answer

    C. always, whether or not there is an exception

    Why: The finally block runs on every exit path: normal completion, a caught exception, an uncaught one, and even a return inside try. That is why cleanup belongs there.

  2. Multiple choiceq0980

    2. Ming writes `except Exception` before `except ValueError`. What happens to the ValueError branch?

    1. it never runs
    2. it still runs first
    3. it raises SyntaxError
    4. it runs twice
    Show answer

    Answer

    A. it never runs

    Why: Handlers are tested top to bottom and the first match wins. Exception covers every ordinary error, including ValueError, so the later specific branch is dead code.

  3. Multiple choiceq0985

    3. 🧋 What happens with a bare raise that has no exception type?

    1. it re-raises the current exception
    2. it raises RuntimeError
    3. it does nothing
    4. it is an error
    Show answer

    Answer

    A. it re-raises the current exception

    Why: A bare raise inside except re-raises the exception being handled and keeps its original traceback. It is the idiom for logging and then letting the caller decide.

  4. Multiple choiceq1000

    4. Converting the user input 'abc' to an integer fails. Which exception is usually raised?

    1. FileNotFoundError
    2. ValueError
    3. KeyError
    4. TypeError
    Show answer

    Answer

    B. ValueError

    Why: int('abc') fails on content, not on type, so ValueError. TypeError would mean you passed something like None or a list. The message names the actual string.

  5. Fill in the blankq0974

    5. Ming wants the file closed even if writing fails. Which keyword opens it safely?

    ____ open('orders.txt', 'w') as f:
        f.write('latte')
    Show answer

    Answer

    with

    Why: The with statement uses the file's context manager, which closes it on normal exit and on exception, replacing a try/finally. The as f part binds the open file.

  6. Fill in the blankq0993

    6. Which keyword catches an exception in a try statement?

    Show answer

    Answer

    except

    Why: except names the exception type to handle and introduces the handler block. try only marks the code being watched. finally is for cleanup, not catching.

  7. Fill in the blankq0998

    7. Raise an exception deliberately. Which keyword goes in the blank?

    if price < 0:
        ____ ValueError('price cannot be negative')
    Show answer

    Answer

    raise

    Why: raise ValueError(...) rejects the bad price with a message the caller can show. The condition guards it, so valid prices pass through.

  8. Output predictionq0973

    8. After an exception is caught, does finally still run? What is printed?

    try:
        print(1 / 0)
    except ZeroDivisionError:
        print("boom")
    finally:
        print("end")
    Show answer

    Answer

    boom
    end

    Why: 1/0 raises ZeroDivisionError, so except prints boom; finally then runs and prints end. The traceback is suppressed because the exception was handled.

  9. Output predictionq0987

    9. A calculator catches a division by zero. What is printed?

    try:
        print(10 / 0)
    except ZeroDivisionError:
        print("safe")
    Show answer

    Answer

    safe

    Why: 10 / 0 raises before print runs, so try produces no output; the handler then prints safe. The failed print never printed anything.

  10. Output predictionq1004

    10. Use a default when conversion fails. What is printed?

    text = 'abc'
    try:
        qty = int(text)
    except ValueError:
        qty = 0
    print(qty)
    Show answer

    Answer

    0

    Why: int('abc') raises ValueError, the handler assigns qty = 0, and print shows 0. qty is defined because the except branch always runs on failure.

Common questions

When does else run?

The else block runs when the try block completes without an exception.

Should I catch Exception?

Usually only at a boundary where you can log, clean up, and re-raise or translate the error. Catch specific exceptions close to where you can handle them.

Keep the streak going.

The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.

Open full practice