Skip to content
Exercises · Q7

Q.Consider the code given below and fill in the blanks.
print(" Learning Exceptions...")  
try:  
    num1 = int(input("Enter the first number"))  
    num2 = int(input("Enter the second number"))  
    quotient = (num1/num2)  
    print("Both the numbers entered were correct")  
except _____________:             # to enter only integers  
    print(" Please enter only numbers")  
except ____________:      # Denominator should not be zero  
    print(" Number 2 should not be zero")  
else:  
    print(" Great .. you are a good programmer")  
___________:               # to be executed at the end  
    print(" JOB OVER... GO GET SOME REST")  

Puducherry TnboardTextbookSubjective· 2mImportance★★★★★
78% · 7/9 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The code demonstrates Python exception handling with try, multiple except blocks, else, and finally — each clause serves a specific purpose in catching and responding to runtime errors.

This is a fill-in-the-blanks question about Python's exception handling mechanism. The key insight is that Python evaluates except blocks in order, so you must catch the more specific exception first, then the more general one.

The first blank needs to catch non-integer input. When int() fails on a string like "abc", it raises a ValueError. That's the exception to catch here — not TypeError or Exception broadly, because ValueError is the exact exception raised when conversion to int fails.

The second blank needs to catch division by zero. In Python, dividing an integer by zero raises ZeroDivisionError. Note that this is a built-in exception specifically for this case.

The third blank needs a clause that always executes, regardless of whether an exception occurred or not. That's the finally clause — it runs after try, except, and else blocks, even if there's a return statement inside the try block.

Watch out

A common mistake is to reverse the order of the except blocks. If you put ZeroDivisionError first and ValueError second, it still works here because they're unrelated exceptions. But if you put a generic Exception before a specific one like ValueError, the specific handler would never run — Python matches the first matching except block.

Here's the completed code:

print(" Learning Exceptions...")
try:
    num1 = int(input("Enter the first number"))
    num2 = int(input("Enter the second number"))
    quotient = (num1/num2)
    print("Both the numbers entered were correct")
except ValueError:             # to enter only integers
    print(" Please enter only numbers")
except ZeroDivisionError:      # Denominator should not be zero
    print(" Number 2 should not be zero")
else:
    print(" Great .. you are a good programmer")
finally:               # to be executed at the end
    print(" JOB OVER... GO GET SOME REST")

Let's trace through the logic: …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.