HiveBrain v1.2.0
Get Started
← Back to all entries
debugpythonCritical

How do I print an exception in Python?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
pythonhowexceptionprint

Problem

How do I print the error/exception in the except: block?

try:
    ...
except:
    print(exception)

Solution

For Python 2.6 and later and Python 3.x:

except Exception as e: print(e)


For Python 2.5 and earlier, use:

except Exception,e: print str(e)

Code Snippets

except Exception as e: print(e)
except Exception,e: print str(e)

Context

Stack Overflow Q#1483429, score: 2036

Revisions (0)

No revisions yet.