Skip to main contentSkip to solution

Arrange the following python code segments in order with respect to exception handling.

(A) except ZeroDivisionError:

print("Zero denominator not allowed")

(B) finally:

print("Over and Out")

(C) try:

n=50

d=int(input("enter denominator")

q=n/d

print("division performed")

(D) else:

print("Result=",q)

Solution

✅ Correct Option: 2

Python requires the blocks in the order try, except, else, finally. The try block (C) comes first, then the except handler (A), then the else block (D), which runs only if no exception occurred, and finally the finally block (B), which always runs last.

(C) try: the code that may raise an error

(A) except ZeroDivisionError: handles the error

(D) else: runs only if the try block raised no exception

(B) finally: runs always, whether or not an exception occurred


Option 1: puts finally before else, which is a syntax error since else must come before finally.

Option 4: puts finally and else before except, which is invalid because except must directly follow try.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question