Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Examples
3.
FAQs
4.
Conclusion
Last Updated: Mar 27, 2024

Raising an Exception to another Exception

Author Yashesvinee V
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Exceptions can be raised by a developer or programmer when they deem it necessary. It allows us to forcefully throw the exception we require along with a message of our choice. Exception chaining is a method that involves raising an exception to another exception. This is done when we want to include information about two different exceptions that may occur.

You can also read about the Multilevel Inheritance in Python and Convert String to List Python.

Examples

In the following example, a runtime error is raised inside the exception handler for ZeroDivisionError. This displays the information of both the errors that occurred during execution.

try:
    a = 10/0
except ZeroDivisionError as e:
    raise RuntimeError('Error') from e

 

Output:

Traceback (most recent call last):
File "main.py", line 2, in <module>
a = 10/0
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "main.py", line 4, in <module>
raise RuntimeError('Error') from e
RuntimeError: Error

 

Exception chaining can also be caused if there is an exception in the except block itself. This is an implicit form of exception chaining. Observe the example shown below.

try:
    a = 5/0
except ZeroDivisionError as e:
    print(err)

 

Output:

Traceback (most recent call last):
File "main.py", line 2, in <module>
a = 5/0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main.py", line 4, in <module>
print(b)
NameError: name 'err' is not defined

 

We can suppress exception chaining by raising the other exception from None. Here is an example.

try:
    a = 5/0
except ZeroDivisionError as e:
    raise RuntimeError('Error') from None

 

Output:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    raise RuntimeError('Error') from None
RuntimeError: Error

 

You can practice by yourself with the help of online python compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

  1. What is the difference between an error and an exception?
    Errors are the problems in a program that causes it to stop its execution. On the other hand, exceptions are raised when some internal events cause disruptions in the normal flow of the program.
     
  2. What is Traceback in Python?
    A traceback is a report containing the executions made in your code at a specific point exception. It provides all the necessary information to resolve an exception in the code. It mentions the name of the exception and the position at which it was thrown.

Conclusion

This article extensively discusses how to raise an exception to another exception in Python. We hope that this blog has helped you enhance your knowledge about raising exceptions in Python. If you would like to learn more, check out our articles try-except for Exception Handling and Raising an Exception in Python. Must Recommended Topic, Floor Division in Python

Explore the Coding Ninjas Library and upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Try and Except in Python
Next article
Handling EOFError Exception in Python
Live masterclass