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

Python Error Handling New

Uploaded by

casualking001
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python Error Handling New

Uploaded by

casualking001
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Error Handling in Python

Syntax Error

• The most common reason of an error in a Python program is


when a certain statement is not in accordance with the
prescribed usage. Such an error is called a syntax error.

• Ex
• Print “hellow”
Exception

• Many times though, a program results in an error after it is


run even if it doesn't have any syntax error. Such an error is
a runtime error, called an exception.
Exception handling

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

Raised when a variable is not found in the local or


NameError
global scope.

NotImplementedError Raised by abstract methods.

Raised when a system operation causes a system-


OSError
related error.

Raised when the result of an arithmetic operation is


OverflowError
too large to be represented.

Raised when a weak reference proxy is used to


ReferenceError
access a garbage collected referent.

Raised when an error does not fall under any other


RuntimeError
category.

Raised by the next() function to indicate that there is


StopIteration
no further item to be returned by the iterator.
Exception handling
Exception Description
Indentation Error Raised when there is an incorrect indentation.
Raised when the indentation consists of inconsistent
TabError
tabs and spaces.
SystemError Raised when the interpreter detects internal error.
SystemExit Raised by the sys.exit() function.
Raised when a function or operation is applied to an
TypeError
object of an incorrect type.
Raised when a reference is made to a local variable
UnboundLocalError in a function or method, but no value has been
bound to that variable.
Exception handling

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 noone


Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>

import no one
ModuleNotFoundError: No module named 'noone’
Key Error

>>> D1={'1':"aa", '2':"bb", '3':"cc"}


>>> D1['4']
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>

D1['4']
KeyError: '4'
Import Error

>>> from math import cube


Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>

from math import cube


ImportError: cannot import name 'cube'
Stop Iteration

>>> 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

>>> name=input('enter your name')


enter your name^c
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>

name=input('enter your name')


KeyboardInterrupt

You might also like