Answer key · 12 questions

Python Error Handling Exercises With Answers

All 12 questions for Python Error Handling in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Error Handling, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

Which exception does int("abc") raise?

  1. ValueErrorcorrect
  2. TypeError
  3. KeyError
  4. IndexError

Answer: ValueError

Why: The text has the right type but an invalid value for int().

02 · Multiple choice

Which exception does a missing dictionary key raise?

  1. NameError
  2. SyntaxError
  3. KeyErrorcorrect
  4. ValueError

Answer: KeyError

Why: Square-bracket access on a missing key raises KeyError.

03 · Multiple choice

What is the main purpose of try/except?

  1. To handle an expected failure and recovercorrect
  2. To convert text to numbers
  3. To hide every bug
  4. To make code run faster

Answer: To handle an expected failure and recover

Why: A handler should catch a specific, predictable failure and choose a fallback.

04 · Multiple choice

When does a finally block run?

  1. Only when the program exits
  2. Always, after try and any except/else blockcorrect
  3. Only after an exception
  4. Only when try succeeds

Answer: Always, after try and any except/else block

Why: finally is used for cleanup that must happen either way.

05 · Fill in the blank

Complete the exception type that handles a bad integer conversion.

try:
    value = int(text)
except ____:
    value = 0

Answer: ValueError

Why: int() raises ValueError when the text cannot be converted.

06 · Fill in the blank

Complete the keyword that begins an exception handler.

try:
    value = int(text)
____ ValueError:
    value = 0

Answer: except

Why: except introduces the block that handles a matching exception.

07 · Fill in the blank

Complete the exception type raised by a missing dictionary key.

try:
    price = order["price"]
except ____:
    price = 0

Answer: KeyError

Why: Reading a missing key with square brackets raises KeyError.

08 · Fill in the blank

Complete the block that always runs for cleanup.

try:
    print("work")
____:
    print("cleanup")

Answer: finally

Why: finally runs whether the try succeeds or fails.

09 · Predict output

What is printed?

try:
    print(int("5"))
except ValueError:
    print("bad")

Answer: 5

Why: The conversion succeeds, so the except block is skipped.

10 · Predict output

What is the output?

try:
    print(int("x"))
except ValueError:
    print("invalid")

Answer: invalid

Why: The conversion fails, so the handler prints invalid.

11 · Predict output

What does this code print?

order = {}
try:
    print(order["price"])
except KeyError:
    print("missing")

Answer: missing

Why: The key is absent, so the KeyError handler runs.

12 · Predict output

What does the program output?

try:
    print("try", end=" ")
finally:
    print("finally")

Answer: try finally

Why: finally always runs, and the first print avoids a newline.

Try the questions first

Open the practice page for Python Error Handling and answer the same 12 questions with instant feedback.

Practice this lesson