Built-in Exceptions in Python
Built-in Exceptions
Some of the python built-in exceptions are illustrated in below.
1.SyntaxError
Raised whenever it detects an error in python syntax.
Ex
>>> if x+5
SyntaxError: invalid syntax >>> print “hi” SyntaxError: Missing parentheses in call to ‘print’ >>> if:
SyntaxError: invalid syntax >>> for:
SyntaxError: invalid syntax >>> print x SyntaxError: Missing parentheses in call to ‘print’ >>> for x:
SyntaxError: invalid syntax >>> print(“hi”,”hello”+) SyntaxError: invalid syntax |
2. ZeroDivisionError
It raises during the division/modulo of a number with zero.
Ex
>>> 1/0
Traceback (most recent call last): File “<pyshell#10>”, line 1, in <module> 1/0 ZeroDivisionError: division by zero >>> p=1 >>> q=0 >>> p/q Traceback (most recent call last): File “<pyshell#13>”, line 1, in <module> p/q ZeroDivisionError: division by zero >>> 1.2/0 Traceback (most recent call last): File “<pyshell#14>”, line 1, in <module> 1.2/0 ZeroDivisionError: float division by zero |
3.NameError
This rises when an identifier is not found in local or global spaces.
Ex
>>> for p in items( ):
print(p)
Traceback (most recent call last): File “<pyshell#2>”, line 1, in <module> for p in items(): NameError: name ‘items’ is not defined >>> print(a) Traceback (most recent call last): File “<pyshell#3>”, line 1, in <module> print(a) NameError: name ‘a’ is not defined >>> tup(10) Traceback (most recent call last): File “<pyshell#4>”, line 1, in <module> tup(10) NameError: name ‘tup’ is not defined >>> math.module Traceback (most recent call last): File “<pyshell#9>”, line 1, in <module> math.module NameError: name ‘math’ is not defined >>> os.dir( ) Traceback (most recent call last): File “<pyshell#10>”, line 1, in <module> os.dir( ) NameError: name ‘os’ is not defined >>> assert sqrt(-64) Traceback (most recent call last): File “<pyshell#6>”, line 1, in <module> assert sqrt(-64) NameError: name ‘sqrt’ is not defined |
4.ValueError
Raised when the built-in function for a specific data type has the valid type of arguments, but the arguments have invalid values specified.
Ex:
>>> var=float(input(“Number:”))
Number: sudha Traceback (most recent call last): File “<pyshell#0>”, line 1, in <module> var=float(input(“Number:”)) ValueError: could not convert string to float: ‘ sudha’ >>> k=int(“Enter a string:”) Traceback (most recent call last): File “<pyshell#1>”, line 1, in <module> k=int(“Enter a string:”) ValueError: invalid literal for int( ) with base 10: ‘Enter a string:’ |
5.TypeError
Raised when an invalid operation or function is attempted. It means attempting an operation which is not valid for a specified data type.
Ex
>>> “12.4”+5
Traceback (most recent call last): File “<pyshell#1>”, line 1, in <module> “12.4”+5 TypeError: Can’t convert ‘int’ object to str implicitly >>> str+”4″ Traceback (most recent call last): File “<pyshell#2>”, line 1, in <module> str+”4″ TypeError: unsupported operand type(s) for +: ‘type’ and ‘str’ >>> int*4 Traceback (most recent call last): File “<pyshell#3>”, line 1, in <module> int*4 TypeError: unsupported operand type(s) for *: ‘type’ and ‘int’ >>> dict={1} >>> dict[2] Traceback (most recent call last): File “<pyshell#7>”, line 1, in <module> dict[2] TypeError: ‘set’ object does not support indexing. >>> d={13,78,98} >>> d.add( ) Traceback (most recent call last): File “<pyshell#6>”, line 1, in <module> d.add( ) TypeError: add( ) takes exactly one argument (0 given) >>> y(1) Traceback (most recent call last): File “<pyshell#4>”, line 1, in <module> y(1) TypeError: ‘tuple’ object is not callable |
6.IndexError
Raised when an index is not found in a sequence.
Ex:
>>> list=[2.4,5,7]
>>> list[5] Traceback (most recent call last): File “<pyshell#5>”, line 1, in <module> list[5] IndexError: list index out of range >>> list=[0,2,4,6] >>> list.append(45) >>> list[9] Traceback (most recent call last): File “<pyshell#21>”, line 1, in <module> list[9] IndexError: list index out of range >>> list=[“”] >>> list[-3] Traceback (most recent call last): File “<pyshell#14>”, line 1, in <module> list[-3] IndexError: list index out of range |
7.AttributeError
Raised in case of failure of attribute reference or assignment.
Ex
>>> dict={1}
>>> dict.keys( ) Traceback (most recent call last): File “<pyshell#17>”, line 1, in <module> dict.keys( ) AttributeError: ‘set’ object has no attribute ‘keys’ >>> dict.values( ) Traceback (most recent call last): File “<pyshell#18>”, line 1, in <module> dict.values( ) AttributeError: ‘set’ object has no attribute ‘values’ >>> y=1,2 >>> y.one Traceback (most recent call last): File “<pyshell#3>”, line 1, in <module> y.one AttributeError: ‘tuple’ object has no attribute ‘one’ |
8.AssertionError
Raised when an Assert statement fails.
Ex:
>>> assert( )
Traceback (most recent call last): File “<pyshell#2>”, line 1, in <module> assert( ) AssertionError >>> s=9 >>> assert s>9 Traceback (most recent call last): File “<pyshell#4>”, line 1, in <module> assert s>9 AssertionError |
9.ImportError
Raised when an import statement fails.
Ex
>>> import error
Traceback (most recent call last): File “<pyshell#7>”, line 1, in <module> import error ImportError: No module named ‘error’ >>> import module Traceback (most recent call last): File “<pyshell#8>”, line 1, in <module> import module ImportError: No module named ‘module’ >>> import os.module Traceback (most recent call last): File “<pyshell#9>”, line 1, in <module> import os.module ImportError: No module named ‘os.module’; ‘os’ is not a package |