lionel319
lionel319

Reputation: 1256

How to get the exception object thrown in the default

I know that we can get the exception object if we use the 'as' syntax:-

try:
    1/0
except ZeroDivisionError as e:
    print "can not divide zero"
    print(str(e))

I'd like to capture everything .... ex:-

try:
    1/0
except * as e:
    print "some error"
    print(str(e))

Can that be done?

Upvotes: 0

Views: 45

Answers (2)

Eric Renouf
Eric Renouf

Reputation: 14490

Catch Exception like

except Exception, e:

Upvotes: 0

Marius
Marius

Reputation: 60060

All of Python's exceptions are subclasses of Exception, so you want:

try:
    1/0
except Exception as e:
    print "some error"
    print(str(e))
# Output:
some error
integer division or modulo by zero

Upvotes: 5

Related Questions