Reputation:
I've read this article http://docs.python.org/2/tutorial/errors.html two times to make sure. It avoids the topic entirely.
Exception
or ExceptionBase
, the interpreter told me I can only throw ExceptionBase
-esque and old-style classes' object.>>> class Foo():
... pass
...
>>> try:
... raise Foo()
... except Exception as foo:
... print 'foo %s' % foo
... except:
... print 'not an exception'
... else:
... print 'it\'s all good'
...
not an exception
>>>
Surprise... So, how do I catch all of them and examine what was caught?
EDIT:
Motivation.
Upvotes: 2
Views: 293
Reputation: 97631
import types
try:
raise Foo()
except (Exception, types.InstanceType) as foo:
print 'foo %s' % foo
else:
print 'it\'s all good'
While that feels like it should work, it doesn't. Here's a hacky way:
import sys
try:
raise Foo()
except:
etype, foo, traceback = sys.exc_info()
print 'foo %s' % foo
else:
print 'it\'s all good'
Upvotes: 3