How can I test if a function returned an exception object if I don't know in advance what type of exception has been raised?
def func():
try:
bad_thing_happens # NameError("global name 'bad_thing_happens' is not defined",)
return True
except Exception as e:
return e
result = func()
if result == Exception:
print 'a bad thing happened'
In this case the if statement returns False and I would miss the exception?
Exceptionis a bad idea. If you don't know the type of exception, you very rarely actually know how to handle it properly... Sometimes the best thing you can do is let your program die, look at the stack trace to figure out why it died and fix the bug/logic so that it doesn't die the next time.