PCEP-30-02 · Question #241
QUESTION 269 What is the expected result of the following code? try: raise Exception except: print("c") except BaseException: print("a") except Exception: print("b")
The correct answer is C. The code will cause a syntax error. Option C is correct because Python enforces a strict ordering rule: a bare except: clause (with no exception type specified) must always be the last except block in a try statement. Placing except BaseException and except Exception after the bare except: violates this rule, and…
Question
Options
- A1
- Ba
- CThe code will cause a syntax error.
- Db
How the community answered
(33 responses)- A9% (3)
- B3% (1)
- C82% (27)
- D6% (2)
Explanation
Option C is correct because Python enforces a strict ordering rule: a bare except: clause (with no exception type specified) must always be the last except block in a try statement. Placing except BaseException and except Exception after the bare except: violates this rule, and Python raises a SyntaxError: default 'except:' must be last at parse time, before any code runs.
Why the distractors are wrong:
- A (1): Nothing in the code ever prints
1; this is a red herring. - B (a):
except BaseExceptionwould only execute if the bareexcept:weren't there - but the code never runs at all due to the syntax error. - D (b): Similarly,
except Exceptionis unreachable for the same reason; execution never begins.
Memory tip: Think of bare except: as a "catch-all net" - once you cast it, there's nothing left to catch, so Python won't let you add more nets after it. If bare except: isn't last, the file won't even load.
Community Discussion
No community discussion yet for this question.