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")
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")
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 →Concept understanding — Raise Statement Exception
Imagine you are writing a set of rules for a game. You expect players to follow them. But what happens if a player tries to make a move that is completely illegal — say, moving a chess piece off the board? In a computer program, when something unexpected or forbidden happens, the program needs a way to say, "Stop! Something went wrong here." That is exactly what a raise statement does.
In programming, a raise statement is the instruction you write to deliberately trigger an error — called an exception — in your code. You are not waiting for an error to happen by accident; you are actively saying, "If this condition is met, treat it as an error and stop normal execution."
Think of it like a referee in a sports match. The referee doesn't wait for a foul to be noticed by the players; they blow the whistle and stop the game. The raise statement is that whistle. It signals that something has violated the rules you set, and the program must now handle that violation.
Why would you ever want to create an error on purpose? Because it gives you control. Instead of letting your program continue with bad data — like a negative price for a product or an empty customer name — you can check for that bad data and raise an exception immediately. This forces the program to stop and deal with the problem, rather than silently producing wrong results later.
Here is how it works in practice:
- You write a condition that checks for something invalid (e.g.,
if age < 0). - Inside that condition, you write
raise ExceptionType("Your message here"). - When that line runs, the program halts normal flow and looks for a matching
exceptblock to handle the error.
The "ExceptionType" is the kind of error you want to raise. Common built-in ones include ValueError (for wrong value), TypeError (for wrong data type), or ZeroDivisionError. You can also create your own custom exception types, but for beginners, using built-in ones is perfectly fine.
The message inside the parentheses is optional but very helpful — it tells anyone reading the error output exactly what went wrong. For example, raise ValueError("Age cannot be negative") is much clearer than just raise ValueError. …
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.