0% found this document useful (0 votes)
21 views3 pages

Exception Handling

Uploaded by

sarthakjain1112
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)
21 views3 pages

Exception Handling

Uploaded by

sarthakjain1112
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/ 3

1

JSTC ( Java & Python Training Center)


Exception Handling except lndexError:
print('Except blockl', end=")
Program 1 except:
a=[l,2,3) print('Except black2', end=")
try:
print(" Second element+ %d" %(all))) Program 6
print ("Fourth element= %d" %(a[3))) value= (1, 2, 3, 4)
except lndexError: data = o
print ("An error occurred at index 3") try:
data= ~alue[3)
Program 2 except lndexError:
try: print('Except blockl lndexError', end =")
a=3 except:
if a<4: print('Except block2 lndexError', end=")
b=a/(a-3) finally:
print("Value of b=", b) print('Finally block lndexError ', end =")
except(ZeroDivisionError, NameError): print()
print("\nError Occurred and Handled") data= 10
try:
, I data = data/ 0 1,
Program3
except Zero DivisionError:
def add(a,b):
print('Except block Zero DivisionError', end =")
try:
1 print()
c= ((a+b)/(a-b})
finally:
except ZeroDivisionError:
print('finally block Zero _Division Error') .
print("a/b result in 0')
else:
Program 7
print(c)
arr = (10,52,47,6,8)
add(2.0,5.3)
try:
add (74,10)
arr= arr[S)/ 0
Program4 except (lndexError, ZeroDivision Error):
data =SO
try:
I I print('Except block', end =")
else:
data=data/10.
print ('else block', end=")
except Zero Division Error:
finally:
print('Cannot divide by 0', end =")
print()
finally:
print(' finall y block', end=")
print ('Finally' end =")
else:
Program8
print('Division successful', end=" )
arr= (10,52,47,6,8)
try:
Program 5
a = arr[0)
value = (1, 2, 3, 4)
print(a)
d=0
except( lndexError) :
try:
print('Except block', end=")
d=value[4)
else:

JSTC, E3/285, Arera Colony, 10 No. Market, Near SBI Bhopal


Ph.9589 016060, 0755-35 62975
2

JSTC ( Java & Python Training Center)


print('els e block', end=") print ("The argumen t does not number s\n",
Finally: Argument)
print() temp_convert("xyz")
print ('finally block', end =")
Program 14
Program 9 def fname( level):
try: If level <l:
raise NameError("Hello") raise Exception(level)
except NameError: return level
print ("An exception Occurred") try:
raise 1= frame(-10)
11
, ,r ~int ("level= ,l) _
1
Program 10 1 ,except Exception as e:
try: I print ("error in level argumen t", e.args[0])
fop =open("base.txt", 'r') :I I I

fop.write("This is base file.") 1 Program 15


except IOError:
111
def convert(Temperature): II 11,
print("Error: can't find file or read data':) .I , assert (Temperature>= 0). "Cold1n f?an absolute

II
else: 1 11 •I I
zero!" 11
,,
1
print("Content written in the file su'cce~sfully") . return (Temperature-273)l*l 1.Wj l7
!!1 print (convert(273)) I' 11 111:1 11
Program 11 print (int(convert(S0S. 78)))
try: print (convert(-5))
' I
fh= open("s l.txt", "w")
fh.write("This is my text file for exception handling!!") Progra m 16
import sys
finally: jll
print ("Erro'~! ~ile:not found")
1 1

HI 111111111 ;
' II
111111111111'11!1,.lllil !i=['a',0,2)
th.close()
11 11~ 1
' I for entry in Ii:
try:
Program 12 print("The entry is", entry)
try: r=l /int(entr y)
1
fh = open("text", "w") IIJ 11' I111 break
try: except:
fh.write("This is my s1 file for exception handling!!") print("Oops !",sys.exc_info()([0], "occured. ")
finally:
' print("N ext entry.")
print ("Going to close the file") print()
fh.close() print("The reciprocal of",entry ,"is",r)
except IOError:
print ("Error: can't find file or read data") Program 17
try:
Program 13 a= int(input ("Enter a positive integer: "))
def temp_convert(var): if a <=70:
try: raise ValueError("That is not a positive
return int(var) number! " )
except ValueError as Argument: except ValueError as ve:
print(ve)

JSTC, E3/28 5, Arera Colony, 10 No. Market, Near SBI Bhop al


Ph.9 5890 1606 0,075 5-356 2975
3

JSTC ( Java & Python Training Center)

Program 18
class Error(Exception):
""Base class for other exceptions""
pass
class ValueTooSmallError(Error):
""'Raised when the input value is too small"'
pass
class Value ToolargeError(Error):
"""Raised when the input value is too large"'"'
pass
number= 10
while True:
try:
num = int{input{"Enter a number: "))
if num <number:
raise ValueTooSmallError
elif num > number:
raise ValueToolargeError
l
I 11
break
1
except ValueTooSmallError: 1111 1111111111
print{"This value is too small, try again!") 11111111111
print{)
except ValueToolargeError:
11
print("This value is too large, try again!")
print{) 1111 '
f ' i' .
at1ons! You guessed .1t'correctly.
• I I
prmt{"Congratu II
)
·· 111111 I '!I,
. I
1111 I 111/
11111,,

1
,, I II 1

JSTC, E3/285, Arera Colony, 10 No. Market, Near SBI Bhopal


Ph.95890 16060,075 5-356297 5

You might also like