Arrange the given keywords in the blank spaces in the correct order of their appearance in the code.
try:
n1= int(input("Enter first number"))
n2= int(input("Enter second number"))
quotient= (n1/n2)
except __________:
print("Please enter numbers only")
except __________:
print("n2 can not be zero")
____:
print("Something went wrong")
____:
print( "Bye")
(A). ZeroDivisionError
(B). ValueError
(C). except
(D). finally
Arrange the given keywords in the blank spaces in the correct order of their appearance in the code.
try:
n1= int(input("Enter first number"))
n2= int(input("Enter second number"))
quotient= (n1/n2)
except __________:
print("Please enter numbers only")
except __________:
print("n2 can not be zero")
____:
print("Something went wrong")
____:
print( "Bye")
(A). ZeroDivisionError
(B). ValueError
(C). except
(D). finally
Solution
The order is (B), (A), (C), (D).
The first handler prints "Please enter numbers only", so it catches ValueError, which int() raises when the input is not a number. The second handler prints "n2 can not be zero", so it catches ZeroDivisionError, raised by n1/n2 when n2 is 0.
The third blank is a general except: that catches any other error ("Something went wrong"). The last blank is finally:, which always runs, so "Bye" prints in every case.
Option 1 and Option 2: ZeroDivisionError cannot come first, because the first message is about non-numeric input, which is a ValueError.
Option 4: except and finally are keywords, not exception names, so they cannot fill the first two blanks after except.
A bare except: must come after the specific handlers. If it came first, it would catch every error and the later handlers would never run.
Related questions:
2026: 23 May Shift 1