Exception Handling in Python

In this tutorial, we will learn about Exception Handling in Python. Before going into the depth we have a very important question, “What is Exception?”. And the answer to this question is very simple. In the normal way we know exception means the abnormal situation. But in python, Exception is an event which occurs while execution of the program and it breaks the normal flow of the program. This thing happens because sometimes our Python script stuck in a situation that it cannot cope with, and it just throws an exception. The exception is a Python object which use to show an error.

What is Exception Handling in Python?

Exception Handling in Python means to protract your Python code from these Exceptions. By handling these exceptions you can save code from falling in these exceptions. How we can do that? Suppose you have written code and you think there is some code which may throw an exception while execution. So in this situation, you can place your suspicious code in try: block. After that, we will add except block. In this block, we will write code which will run in case of exception occur and this code will help us to handle this abnormal situation.

List of Standard Exceptions in Python?

Here is a list of some common Standard Exceptions in Python which you will normally see while working in Python.

Exception Description
Exception This is base class of all other Exception classes.
SystemExit This Exception is occur by sys.exit().
StopIteration when next() method of iteration doesn’t point to any object.
ArithmeticError ArithmeticError in base class of all the numeric calculation errors
OverflowError This exception occurs when normal calculation exceeds maximum limit of numeric type.
FloatingPointError This exception occurs There is some error in calculation of floating numbers.
ZeroDivisionError This exception occurs when we try to divide a numeric by zero.
ImportError This exception occurs import statement get fail.
IndexError This exception occurs when there is any problem while finding index of a squence.
NameError This exception occurs when there is any problem finding a identifier.
IOError This exception occurs when there is any I/O operation get fail.
SyntaxError This exception occurs when use wrong Python Syntax.
NotImplementedError This exception occurs when we forget to implement a abstract method.
NotImplementedError This exception occurs when we forget to implement a abstract method.
Spread the love
Scroll to Top
×