nerdexam
Python_Institute

PCEP-30-02 · Question #230

Which of the approachable except: branches is taken into consideration when an exception occurs?

The correct answer is B. The first matching branch. When an exception occurs, Python (and most languages with try/except or try/catch blocks) evaluates the except branches top to bottom and executes the first one whose type matches the raised exception - then stops, ignoring all remaining branches. This means option B is…

Question

Which of the approachable except: branches is taken into consideration when an exception occurs?

Options

  • AThe last matching branch.
  • BThe first matching branch.
  • CAny of the matching branches.

How the community answered

(37 responses)
  • A
    8% (3)
  • B
    73% (27)
  • C
    19% (7)

Explanation

When an exception occurs, Python (and most languages with try/except or try/catch blocks) evaluates the except branches top to bottom and executes the first one whose type matches the raised exception - then stops, ignoring all remaining branches. This means option B is correct.

Option A is wrong because the last matching branch is never sought; evaluation halts as soon as a match is found, so later branches are never even checked.

Option C is wrong because only one branch executes per exception - never multiple. Once the first match fires, control passes to the code after the entire try/except block.

Memory tip: Think of except branches like a bouncer checking a guest list from top to bottom - the moment a match is found, the guest gets in and the rest of the list is ignored. Always put more specific exception types before broader ones, or the broad catch-all will grab everything first and your specific handlers will never run.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice