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

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!