Social Media

2/11/2019

Implicitly raising and handling of Exception

Implicitly raising and implicitly handling:

a=int(input("Enter first number "))
b=int(input("Enter second number "))
rslt=a//b
print("The result is",rslt)


If b=0 then result can be as:
Enter first number 2
Enter second number 0
Traceback (most recent call last):
  File "/root/Desktop/p1.py", line 3, in <module>
    rslt=a//b
ZeroDivisionError: integer division or modulo by zero


if b!=0 then result will be:
Enter first number 2
Enter second number 1
The result is 2


Implicitly raising and explicitly handling:

a=int(input("Enter first number "))
b=int(input("Enter second number "))
try:
    rslt=a//b
    print("The result is",rslt)
except ZeroDivisionError as e:
    print(e)
else:
    print("Program executed successfully")
finally:
    print("This program is created by Mayank Kumar Prajapati")


if b=0 then the result of b=above program is:
Enter first number 2
Enter second number 0
integer division or modulo by zero
This program is created by Mayank Kumar Prajapati


if b!=0 then result is:
Enter first number 2 
Enter second number 1
The result is 2
Program executed successfully
This program is created by Mayank Kumar Prajapati

Be First to Post Comment !
Post a Comment