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)
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
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.
Related questions:
2026: 26 May Shift 1
2026: 31 May Shift 1
2026: 29 May Shift 2