Python: Catch Any Exception And Put It In A Variable
Solution 1:
If you want the stacktrace included:
try:# somethingexcept:
the_type, the_value, the_traceback = sys.exc_info()
later
raise the_type, the_value, the_traceback
(Related to this answer)
See also here for Python 2.7.
Solution 2:
In this case, I think I would argue that you may not be doing it right.
To me, the point of an exception is to signal a bum bum baaaaaexceptional circumstance. And when you are writing code that may fire an exception you have two responsible options - catch it and do something with it (like recover), or completely ignore it.
From what you say in your post, you don't really care that an exception happened. It shouldn't halt your program, and program flow should continue as normal. You do however want to know that an exception happened. And this is where the logging module comes in:
importlogginglog= logging
def get_some_cheese():
raise ValueError("Sorry, we're right out.")try:
get_some_cheese()
except:
log.exception("What a waste of life")
When you log an exception it automagically adds stack trace information for you. After you config your logging a bit, you can get it setup to do all sorts of whatever things you want to do - send an email, write to a file or stdout/err, whatever. But then you'll get informed that an exception occured, but you can also simply recover from the error and continue on your merry way cleaning up whatever it is you need to clean up.
Solution 3:
I'd probably use BaseException
to catch anything that gets thrown, and iterate through all your cleanup functions (instead of using recursion). Then append any exceptions to a list, to deal with (re-raise, log, etc) as appropriate when you finish the cleanup.
defrunCleanup(procs):
exceptions = []
for proc in procs:
try:
proc.terminate()
proc.wait()
except BaseException as e:
exceptions.append(e) # Use sys.exc_info() for more detailreturn exceptions # To be handled or re-raised as needed
Solution 4:
Openstack does something very similar for a different reason. Have a look at https://github.com/openstack/nova/blob/master/nova/openstack/common/excutils.py#L30 (function save_and_reraise_exception
). In their case it works like a resource manager.
Solution 5:
Like just about everything else in Python, exceptions are objects and therefore can be bound to names and operated upon. Here's a short example showing how to grab an exception and use it later:
>>>defto_int(x):...try:...returnint(x)...except Exception, e:...print'in exception block:', e...print'after exception block:', e>>>to_int('12')
12
>>>to_int('abc')
in exception block: invalid literal for int() with base 10: 'abc'
after exception block: invalid literal for int() with base 10: 'abc'
Post a Comment for "Python: Catch Any Exception And Put It In A Variable"