0% found this document useful (0 votes)
29 views

March 17 (Exception Handling in Python)

Uploaded by

vshwshwr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

March 17 (Exception Handling in Python)

Uploaded by

vshwshwr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Please Like, Share and Subscribe our YouTube Channel :

http://www.youtube.com/swatichawlaofficial

Exception Handling in Python


For Video Explanation of this topic, please click on the
following link:
https://youtube.com/live/B0usdFChtHw
#Run Time Error
a=int(input("Enter First number:"))
b=int(input("Enter Second Number:"))
c=a/b
print("Division=",c)

f=open("Class12.txt","r")
data=f.read()
print(data)

#Handling Run Time Errros using try.... except block


a=int(input("Enter First number:"))
b=int(input("Enter Second Number:"))
try:
c=a/b
print("Division=",c)
except ZeroDivisionError:
print("Can't divide by Zero...")

try:
f=open("Class12.txt","r")
data=f.read()
print(data)
except FileNotFoundError:
print("File not exist...")

#Handling Multiple Exceptions...(using multiple


except block)

try:
a = int(input("Enter First number:"))
b = int(input("Enter Second Number:"))

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

c=a/b
print("Division=",c)
except ZeroDivisionError:
print("Can't divide by Zero...")
except ValueError:
print("Number must be integer...")

#Handling multiple Exceptions using one except block


only...
#Method 1
try:
a = int(input("Enter First number:"))
b = int(input("Enter Second Number:"))
c=a/b
print("Division=",c)
except:
print("Oops!!! Something went wrong... ")

#Method 2
try:
a = int(input("Enter First number:"))
b = int(input("Enter Second Number:"))
c=a/b
print("Division=",c)
except Exception:
print("Oops!!! Something went wrong... ")

#Using else block...

try:
a = int(input("Enter First number:"))
b = int(input("Enter Second Number:"))
c=a/b
except:
print("Oops!!! Something went wrong... ")
else:
print("Division=",c)

try:
a = int(input("Enter First number:"))
b = int(input("Enter Second Number:"))

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

c=a/b
print("Division=",c)
except ZeroDivisionError:
print("Can't divide by Zero...")
except ValueError:
print("Number must be integer...")
else:
print("Program executed Successfully... 😊")

#Using finally block..

try:
a = int(input("Enter First number:"))
b = int(input("Enter Second Number:"))
c=a/b
print("Division=",c)
except ZeroDivisionError:
print("Can't divide by Zero...")
except ValueError:
print("Number must be integer...")
else:
print("Program executed Successfully... 😊")
finally:
print("End of the Program...")

#Interesting Fact.....
try:
a = int(input("Enter First number:"))
b = int(input("Enter Second Number:"))
c=a/b
print("Division=",c)
finally:
print("End of the Program...")

#Practice Question
try:
a=10
print(a)
except:
print("Error....")

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

#Practice Question
x = 5
y = 0
print('A')
try :
print('B')
a = x / y
print('C')
except:
print('D')

#Find the Output


A=0
B=16
print('One')
try:
print('Two')
X=28/A
print('Three')
except ValueError:
print(B*2)
print('Four')
else:
print(B*3)
print ('Five')
finally:
print("End of the Program")

To get access to Premium Notes, please join the


membership of the channel.
https://www.youtube.com/channel/UC2vH9rqGh-
fJELF8acGfBdw/join

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla

You might also like