Advertisement



< Prev
Next >



Python - Exception Handling





What is an Exception in Python?


Exception in Python language is an unusual exceptional event(describing an error), which occurs only during the runtime of a program.

During the execution of a program, when an exception occurs, it is said to be thrown by the Python interpreter.

If this thrown exception is not handled, it stops the normal execution of the program and it ends abruptly. That's why we should handle an exception when it is thrown by the program. The process of handling an exception is called Exception Handling.




Some situations due to which an Exception may arise :






What happens when an Exception is thrown?


Exceptions are thrown at the runtime of a program. If an exception, which is thrown during the runtime of a program, is not handled, the program terminates right away without executing the remaining statements. Let us understand this by an example here.



Looking at the diagram above, it is easy to understand that if an exception is thrown when statement2 is executed, the program ends abruptly without executing any statement after it. Hence, statement3 and statement4 will not be executed as a result of an exception at statement2.



Advertisement




Some important types of Exceptions -:


In the table below, we have given a short preview about some important exceptions.

Exception Class Description
BaseException
This is a base class for all built-in exceptions.

KeyboardInterrupt
This type of exception is raised when a user pressed CTRL+C to interrupt a program's execution.

SystemExit
This type of exception is raised by sys.exit() function.

Exception
This is a base class for all the built-in exceptions, except system exiting exceptions. User defined exceptions should be derived from this class.

StandardError
This is a base class for all the built-in exceptions except StopIteration, GeneratorExit, KeyboardInterrupt and SystemExit classes.

BufferError

This type of exception is raised during failure of buffer related processing.

ArithmeticError

This is a base class for all built-in exceptions that are raised for arithmetic errors

FloatingPointError
This type of exception is raised during a floating point error.

OverflowError
This type of exception is raised when result of an arithmetic expression is too large to be displayed.

ZeroDivisionError
This type of exception is raised when a number is divided by a zero.

AssertionError
This type of exception is raised during the failure of an assert statement.

AttributeError
This type of exception is raised during the failure of an attribute reference or assignment.

EnvironmentError
This type of exception can occur outside the Python environment system.

IOError
This type of exception is raised during the failure of an I/O operation.

OSError
This type of exception is raised during an operating system related error.
EOFError
This type of exception is raised when EOF(end-of-file) is reached during the call to one of the built-in functions - input() or raw_input() functions, without reading any data.

ImportError
This type of exception is raised during the failure of import statement to find and import a module's definition.

LookupError
This is a base class for all exceptions raised due an invalid key or index used to mapping.

IndexError
This type of exception is raised when a searched index is not found or is out-of-range.

KeyError
This type of exception is raised when a searched key is not found.
MemoryError
This type of exception is raised when an operation runs out of memory.

NameError
This type of exception is raised when a local or global name is not found or its value is not defined.

UnboundLocalError
This type of exception is raised when a local variable is referenced in a function but there is no value bound to it.

RuntimeError
This type of exception is raised when a detected error does not fall in any category.

NotImplementedError
This type of exception is raised when a function of abstract class is not implemented in a derived class.

SyntaxError
This type of exception is raised when there is a syntax error in Python's script/program.

IndentationError
This type of exception is raised during an erroneous indentation in a program.

TabError
This type of exception is raised during an incorrect use of tab and spaces in indentation.

SystemError
This type of exception is raised when Python's interpreter find an internal error but still does not quit or exit.

TypeError
This type of exception is raised when an operation or function is executed over an object of an invalid data type.

ValueError
This type of exception is raised when an operation function recieves an argument whose value does not match with its specified type.

UnicodeError
This type of exception is raised during an error in Unicode coding/decoding.

KeyError
This type of exception is raised when a searched key is not found.



In the next few articles, we will discuss some very important keywords which are used to handle exceptions - try, except finally and raise.



Please share this article -





< Prev
Next >
< Python two dimensional array
Python Exception propagation >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement