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
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming must close the shop whether or not the milk tea succeeds. When does finally run in try-except-finally?
Show answer
Answer
C. always, whether or not there is an exceptionWhy: 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. Ming writes `except Exception` before `except ValueError`. What happens to the ValueError branch?
Show answer
Answer
A. it never runsWhy: 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. 🧋 What happens with a bare raise that has no exception type?
Show answer
Answer
A. it re-raises the current exceptionWhy: 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. Converting the user input 'abc' to an integer fails. Which exception is usually raised?
Show answer
Answer
B. ValueErrorWhy: 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. 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
withWhy: 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. Which keyword catches an exception in a try statement?
Show answer
Answer
exceptWhy: 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. Raise an exception deliberately. Which keyword goes in the blank?
if price < 0: ____ ValueError('price cannot be negative')Show answer
Answer
raiseWhy: raise ValueError(...) rejects the bad price with a message the caller can show. The condition guards it, so valid prices pass through.
-
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. A calculator catches a division by zero. What is printed?
try: print(10 / 0) except ZeroDivisionError: print("safe")Show answer
Answer
safeWhy: 10 / 0 raises before print runs, so try produces no output; the handler then prints safe. The failed print never printed anything.
-
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
0Why: 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.