PCEP-30-02 · Question #234
QUESTION 261 The unnamed except block ...
The correct answer is C. must be the last one. In Python, the bare except: clause (with no exception type specified) must always appear last in a sequence of except blocks - this is enforced as a syntax rule by the interpreter, and placing it before any named except causes a SyntaxError. Option A is wrong for the same…
Question
Options
- Amust be the first one.
- Bcannot be used if any named block has been used.
- Cmust be the last one.
- Dcan be placed anywhere.
How the community answered
(33 responses)- A3% (1)
- B12% (4)
- C76% (25)
- D9% (3)
Explanation
In Python, the bare except: clause (with no exception type specified) must always appear last in a sequence of except blocks - this is enforced as a syntax rule by the interpreter, and placing it before any named except causes a SyntaxError. Option A is wrong for the same reason: putting it first would shadow all named handlers that follow, making them unreachable, so Python simply forbids it. Option B is wrong because you can combine a bare except with named blocks - the only constraint is ordering, not prohibition. Option D is wrong because placement is strictly constrained, not free-form.
Memory tip: Think of except: as a "catch-all dragnet" - you always throw the specific, targeted traps first, and only then drop the wide net last. If the dragnet came first, nothing would reach the targeted traps.
Community Discussion
No community discussion yet for this question.