Arrange the following Python Code in order:
(A) try:
numerator=50
denom=int(input("Enter the denominator: "))
quotient=(numerator/denom)
print ("Division performed successfully")
(B) finally:
print ("OVER AND OUT")
(C) else:
print ("The result of division operation is ", quotient)
(D) except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
Arrange the following Python Code in order:
(A) try:
numerator=50
denom=int(input("Enter the denominator: "))
quotient=(numerator/denom)
print ("Division performed successfully")
(B) finally:
print ("OVER AND OUT")
(C) else:
print ("The result of division operation is ", quotient)
(D) except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
Solution
The order of a Python exception-handling block is try, except, else, finally.
(A) try: holds the code that may raise an error
(D) except ZeroDivisionError: runs only if the error occurs
(C) else: runs only if the try block raised no exception
(B) finally: always runs last, whether or not an exception occurred
Option 1: else cannot come before except. Python gives a syntax error.
Option 2: finally must be the last clause, so it cannot come before else.
Memory trick: try, except, else, finally (TEEF).
Related questions:
2025: 15 May Shift 2
2025: 20 May Shift 1
2025: 16 May Shift 1