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.

ExceptionThis is base class of all other Exception classes.
SystemExitThis Exception is occur by sys.exit().
StopIterationwhen next() method of iteration doesn’t point to any object.
ArithmeticErrorArithmeticError in base class of all the numeric calculation errors
OverflowErrorThis exception occurs when normal calculation exceeds maximum limit of numeric type.
FloatingPointErrorThis exception occurs There is some error in calculation of floating numbers.
ZeroDivisionErrorThis exception occurs when we try to divide a numeric by zero.
ImportErrorThis exception occurs import statement get fail.
IndexErrorThis exception occurs when there is any problem while finding index of a squence.
NameErrorThis exception occurs when there is any problem finding a identifier.
IOErrorThis exception occurs when there is any I/O operation get fail.
SyntaxErrorThis exception occurs when use wrong Python Syntax.
NotImplementedErrorThis exception occurs when we forget to implement a abstract method.
NotImplementedErrorThis exception occurs when we forget to implement a abstract method.

Spread the love