Errors and Exceptions - Sheetal Taneja
Errors and Exceptions - Sheetal Taneja
a file
sy n ta x er r or : v iola tion of Py th on g r a m m a r r u le
>>> print('Hello)
In den ta tion er r or
1 . NameError
Th is ex ception occu r s w h en ev er a n a m e th a t a ppea r s
in a sta tem en t is n ot fou n d g loba lly . For ex a m ple, in
th e follow in g sta tem en t, w e in ten d to ta ke m a r ks a s a n
in pu t fr om th e u ser . For doin g so, w e in ten ded to u se
fu n ction input bu t in stea d ty ped Input. Py th on bein g
ca se-sen sitiv e fa ils to r ecog n ize th e fu n ction input a n d
th e sy stem r espon ds w ith th e er r or m essa g e
NameError: name 'Input' is not defined. Th is
m essa g e beg in s w ith th e n a m e of th e ex ception . Note
th a t th e follow in g Traceback object descr ibes th a t
er r or occu r r ed in lin e 1 in Py th on sh ell, in th e m ost
r ecen t ca ll:
name not f ou nd gl ob al l y
>>> print(price)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
print(price)
NameError: name 'price' is not defined
2 . TypeError
Th is ex ception occu r s w h en a n oper a tion or fu n ction is
a pplied to a n object of in a ppr opr ia te ty pe. For ex a m ple,
th e ex pr ession 'sum of 2 and 3 is ' + 5 in v olv es
a ddin g a n u m ber to a str in g w h ich is n ot a v a lid
oper a tion r esu ltin g in a n ex ception .
Inv al i d t y p e of op er ands f or t h e op er at i on
3 . ValueError
Th is ex ception occu r s w h en ev er a n in a ppr opr ia te
a r g u m en t v a lu e, ev en th ou g h of cor r ect ty pe, is u sed
in a fu n ction ca ll, for ex a m ple:
Inv al i d ar gu ment v al u e
>>> int('Hello')
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
int('Hello')
ValueError: invalid literal for int() with
base 10: 'Hello'
at t emp t t o di v i de b y zer o
>>> 78/(2+3-5)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
78/(2+3-5)
ZeroDivisionError: division by zero
5 . OSError
Th is ex ception occu r s w h en ev er th er e is a sy stem
r ela ted er r or su ch a s disk fu ll or a n er r or r ela ted to
in pu t/ou tpu t, for ex a m ple, open in g a n on -ex isten t file
for r ea din g or r ea din g a file open ed in w r ite m ode:
sy st em r el at ed er r or
>>> f = open('passwordFile.txt')
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
f = open('passwordFile.txt')
FileNotFoundError: [Errno 2] No such file or
directory: 'passwordFile.txt'
6 . IndexError
Th is ex ception occu r s w h en ev er w e tr y to a ccess a n
in dex th a t is ou t of a v a lid r a n g e. For ex a m ple, let u s
n a m e th e list of color s ['red', 'green', 'blue'], a s
colors. Now th e v a lid r a n g e of in dex es for colors is
[-3, -2, -1, 0, 1, 2] a n d th e v a lid in dex r a n g e of
in dex es for th e str in g colors[2] is [-4, -3, -2, -1,
0, 1, 2, 3]. A ccessin g a n in dex ou tside a v a lid r a n g e
w ill ca u se IndexError exception to occu r :
ac c essi ng an i nv al i d i ndex
(<class 'FileNotFoundError'>,
FileNotFoundError(2, 'No such file or
directory'), <traceback object at
0x02EB2530>)
> >>
(<class 'ZeroDivisionError'>,
ZeroDivisionError('float division by
zero',), <traceback object at 0x039BD2B0>)
except:
(<class 'AssertionError'>,
AssertionError(), <traceback object at
0x0356D328>)
(<class 'AssertionError'>,
AssertionError(), <traceback object at
0x03B42508>)
>>>
Bye
main()
> >>
Bye
EXERCISES