How to print an exception in Python


Let us take a look at how to print an exception in Python 2x and 3x versions with examples.


Example: Python 2.x
# division by zero exception

try:
    result = 10 / 0  
except Exception as e:
    print str(e)
Output:

division by zero



Example: Python 3.x
# division by zero exception

try:
    result = 10 / 0  
except Exception as e:
    print(str(e))
Print Exception in Python Example

Read More:


Doc: Handling Exceptions

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap