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: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap