What is an exception and how to handle exception

What is an exception

Exceptions are used to represent an exceptional or illegal state of a program. For example, divide by zero is an exception, file not found is an exception too.

Python defines all kinds of exception we can use to indicate exceptional conditions. The following table lists some buildin exceptions.

Class Name Description
Exception The root class for all exceptions
AttributeError Raised when attribute reference or assignment fails
IOError Raised when trying to open a nonexistent file (among other things)
IndexError Raised when using a nonexistent index on a sequence
KeyError Raised when using a nonexistent key on a mapping
NameError Raised when a name (variable) is not found
SyntaxErrorRaised when the code is ill-formed
TypeError Raised when a built-in operation or function is applied to an object of the wrong type
ValueError Raised when a built-in operation or function is applied to an object with correct type, but with an inappropriate value
ZeroDivisionError Raised when the second argument of a division or modulo operation is zero




















Home »
  Python »
    Advanced Features »




Exception Handling
File
Module