NEXT TUTORIAL >> Types of Exceptions in Python
What is an Exception ?
Exception in Python is considered as the runtime error that can be handled by a developer using various techniques of exception handling in Python.
Exceptions can be suppressed or handled using try, except, and finally block in Python. This mechanism of handling runtime errors is termed as Exception Handling in Python.
Why Exception Handling ?
The main aim of Exception Handling is to make the program robust. Robust means reliable. When we handle exceptions in our code, we make sure that when there is an error, our program logs it and continue the execution.
Exception Handling is one of the best practice in the process of software development.
Analysis on Exception Handling in Python
All exceptions are represented as classes in Python. Built-in Exceptions are the exceptions that are already available in Python like OSError.
Base class (or parent class) for all built-in exceptions is BaseException class. This class is further inherited by Exception class. From Exception class, the subclasses ‘StandardError‘ and ‘Warning‘ is derived.

Exceptions are defined as subclasses under StandardError. These Exceptions are mandatory to be handled or else our program will not execute.
On the other hand, Warning represents the caution and does not interrupts the program flow, if left unhandled.
Python also provides the utility to create User Defined Exceptions. When a developer creates his/her own exception class, then, this class should be derive from Exception class.
Steps to handle an Exception in Python
1. Cover the lines of code that have the possibility of throwing the exception under try block.
try:
# statements
Whenever an exception occurs in the code written inside try block, then, program will not be terminated. Rather, PVM starts checking for except block which is responsible to handle the exception.
2. Use of except block to handle the exception caught. (Find in detail below)
3. Lastly, all the mandatory actions like closing of a file, closing a database connection is usually written inside finally block. This is due to the fact that the statements inside the finally block are executed irrespective of whether there is an exception or not.
try:
# statements
except ExceptionName:
# statements
finally:
# statements
This is how you handle exceptions in Python.
Please note that, handling an exception does not mean that you are preventing it but you are just preventing the damage it may cause.
Let’s see one example code to handle some popular exceptions in Python,
def find_divisor(n1, n2):
try:
res = n1/n2
print('Result is : ', res)
print('All went good !!')
except ArithmeticError:
print('No worries, we have handled the exception')
finally:
print('In the finally block')
print('Out of try-except-finally\n')
print('******First Call******')
find_divisor(20, 10)
print('******Second Call******')
find_divisor(20, 0)
O/P
******First Call******
Result is : 2.0
All went good !!
In the finally block
Out of try-except-finally
******Second Call******
No worries, we have handled the exception
In the finally block
Out of try-except-finally
Observe the above code carefully, our first function call is exception-free and we can see proper output. But, in our second call, we deliberately passing 0 as our denominator, which indeed is ‘ZeroDivisionError‘. Hence, we didn’t got any result.
Note that finally block was executed in both cases.
Remove try-except block from above code, see what happens.
Printing the Type of Exception
Instead of printing things manually, we can simply print the name of the exception that was originally caught. To do this, modify except block as:
except Exception as e:
print(e)
In the above method, we are catching an exception as object and then printing it.
Important points on Exception Handling
* We can have multiple except blocks within a same try block.
try:
# statements
except Exception1:
# handler
except Exception2:
# handler
except Exception3:
# handler
except (Exception4, Exception5):
# handler
finally:
# statements
* try-except-else-finally also exists.
If no exception is raised then the statements inside else statements are executed.
try:
# statements
except Exception1:
# handler
except Exception2:
# handler
except Exception3:
# handler
else:
# statements
finally:
# statements
* We can not write except with try, but vice-versa is possible.
Truth has been said.
* finally block is optional to use
Truth has been said.
That’s all here in ‘Exception Handling in Python’.
NEXT TUTORIAL >> Types of Exceptions in Python
