Python Error Handling New
Python Error Handling New
Syntax Error
• Ex
• Print “hellow”
Exception
Exception Description
AssertionError Raised when the assert statement fails.
Raised on the attribute assignment or reference
AttributeError
fails.
Raised when the input() function hits the end-of-file
EOFError
condition.
FloatingPointError Raised when a floating point operation fails.
GeneratorExit Raised when a generator's close() method is called.
ImportError Raised when the imported module is not found.
IndexError Raised when the index of a sequence is out of range.
KeyError Raised when a key is not found in a dictionary.
Raised when the user hits the interrupt key (Ctrl+c or
KeyboardInterrupt
delete).
Exception handling
Exception Description
Exception Description
Raised when a Unicode-related error occurs during
UnicodeTranslateError
translation.
Raised when a function gets an argument of correct
ValueError
type but improper value.
Raised when the second operand of a division or
ZeroDivisionError
module operation is zero.
Raised when a Unicode-related encoding or
UnicodeError
decoding error occurs.
Raised when a Unicode-related error occurs during
UnicodeEncodeError
encoding.
Raised when a Unicode-related error occurs during
UnicodeDecodeError
decoding.
Index Error- Raised when the index of a
sequence is out of range- example.
>>> L1=[1,2,3]
>>> L1[3]
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
L1[3]
IndexError: list index out of range
Module Not Found Error
import no one
ModuleNotFoundError: No module named 'noone’
Key Error
D1['4']
KeyError: '4'
Import Error
>>> it=iter([1,2,3])
>>> next(it)
1
>>> next(it)
2
>>> next(it)
3
>>> next(it)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
next(it)
Stop Iteration
TypeError
>>> '2'+2
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
'2'+2
TypeError: must be str, not int
Value Error
>>> int('xyz')
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
int('xyz')
ValueError: invalid literal for int() with base 10: 'xyz'
Name Error
>>> age
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
age
NameError: name 'age' is not defined
Zero Division Error
>>> x=100/0
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
x=100/0
ZeroDivisionError: division by zero
Keyboard Interrupt