Skip to content
Exercises · Q3

Q.What is the use of a raise statement? Write a code to accept two numbers and display the quotient. Appropriate exception should be raised if the user enters the second number (denominator) as zero (0).

Tamil Nadu DgeTextbookSubjective· 3mImportance★★★★★
33% · 3/9 Questions
✓ Free question

The raise statement explicitly throws an exception when a specific condition is violated, allowing you to enforce constraints and provide meaningful error messages rather than letting the program crash silently or with a cryptic default error.

Why raise is the right tool here

When you divide two numbers, Python will automatically throw a ZeroDivisionError if the denominator is zero. But sometimes you want to catch that condition before the division happens — to give a clearer message, to log the error, or to enforce business logic. The raise statement lets you throw an exception on your own terms.

In this problem, we check whether the denominator is zero and, if so, explicitly raise a ZeroDivisionError with a custom message. This makes the error handling intentional and the feedback to the user more informative.

The code

def divide_numbers():
    try:
        num1 = float(input("Enter the first number (numerator): "))
        num2 = float(input("Enter the second number (denominator): "))
        
        # Explicitly check for zero denominator and raise exception
        if num2 == 0:
            raise ZeroDivisionError("Denominator cannot be zero!")
        
        quotient = num1 / num2
        print(f"The quotient is: {quotient}")
    
    except ValueError:
        print("Invalid input! Please enter numeric values.")
    except ZeroDivisionError as e:
        print(f"Error: {e}")

# Call the function
divide_numbers()

Key lines explained

if num2 == 0: raise ZeroDivisionError("Denominator cannot be zero!")

This is the heart of the solution. Before attempting the division, we test the denominator. If it is zero, we throw a ZeroDivisionError ourselves, with a message that tells the user exactly what went wrong. Without this explicit raise, Python would throw the same exception type during num1 / num2, but our version gives us control over when and how the error is reported.

except ZeroDivisionError as e:

We catch the exception (whether raised by us or by Python) and print the custom message. The as e binds the exception object so we can access its message.

except ValueError:

If the user types something that cannot be converted to a float (like "abc"), float(input(...)) raises a ValueError. We catch that separately to give a different, appropriate message.

Sample run (denominator is zero)

Enter the first number (numerator): 10
Enter the second number (denominator): 0
Error: Denominator cannot be zero!

Sample run (valid input)

Enter the first number (numerator): 15
Enter the second number (denominator): 3
The quotient is: 5.0

Sample run (invalid input)

Enter the first number (numerator): abc
Invalid input! Please enter numeric values.
Watch out

A common mistake is to forget that input() returns a string. If you write num2 = input(...) and then test if num2 == 0, the comparison will always be False because "0" (a string) is not equal to 0 (an integer). Always convert to int or float first.

✓Final answer

The raise statement explicitly throws an exception when a condition is violated. The code above accepts two numbers, checks if the denominator is zero, raises ZeroDivisionError("Denominator cannot be zero!") if so, and otherwise displays the quotient.

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.