Advertisements
Advertisements
Question
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).
Short/Brief Note
Advertisements
Solution
The raise statement can be used to throw an exception.
The syntax of raise statement is: raise exception-name[(optional argument)]
The argument is generally a string that is displayed when the exception is raised.
code to accept two numbers and display the quotient.
n = int(input("Enter Number 1 :"))
m = int(input("Enter Number 2 :"))
if m == 0:
raise ZeroDivisionError
else:
print("Quotient : ", n / m)shaalaa.com
Raising Exceptions in Python
Is there an error in this question or solution?
