Class 11 CBSE - Python Exception Handling Programs

Python is designed to understand the user’s responsibility, the protocol, and the ethical issues related to the field of computing.
Python in class 11 for cbse is very handy when it comes to practice on computers but for the beginers there might be some errors and problems such as exception. But it's always advisable to practice program on computer than writing it on paper because it cherishes your practical skills , knowledge and typing speed and you'll come to know new things and if you want to be a software developer someday then its a must call for you.
Program 1: Write a program that asks user to enter numerator and denominator and displays the quotient and throws an exception if user enters zero as a denominator.
Solution:
num = int(input("Enter the numerator : "))
deno = int(input("Enter the denominator : "))
try:
    quo = num/deno
    print("QUOTIENT :", quo)
except ZeroDivisionError:
    print("Denominator cannot be zero")



                        

Program 2: Write a program that prompts the user to enter a number. If the number is positive or zero print it, otherwise raise an exception.
Solution:
try:
num = int(input("Enter the number : "))
if num>=0:
    print(num)
else:
    raise ValueError("Negative number is not allowed")


except ValueError as e:
print(e)



                        

Program 3: Write a program that prompts the user to enter the age, check if the age is greater than or equal to 18 then display the message "Congrats !! eligible for voting" , otherwise raise an exception
Solution:
try:
age = int(input("Enter the age : "))
if age>=18:
    print("Congrats !! Eligible for voting")
else:
    raise ValueError("You are not eligible for voting")


except ValueError as e:
print(e)


                        

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com