Exception Handling
Exception Handling
Exception
Example
a=input(“Enter a number”)
x=a/5
print(x)
Enter a number 45
TypeError : unsupported operand type(s) for ‘str’ and ‘int’
Types of exception
Some of the commonly occurring built-in exceptions are
Example (ii)
Without using try except block
a=int(input(“Enter a number”))
b=int(input(“Enter a number”))
x=a/b
print(x)
Run:
Enter a number 5
Enter a number 0
x=a/b
ZerDivisionError: division by zero
Ans.
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")