How to generate a python exception with full stacktrace / details?

Viewed 28

I want to capture and log (not print) an exception, my current approach is to raise and catch at the same time:

try:
    raise MyException("Ops!")
except MyException as e:
    do_stuff(e)

The reason why I am not simply calling do_stuff(MyException("...")) is because I need a full stacktrace associated with when it happened.

Is there a more "pythonic" way to do that like do_stuff(raised MyException("..."))?

Extra examples:

# @Bloodbee (comments) -- this will not work, no stacktrace 
e = Exception("Ops")
logger.exception(e)

--

I did my searching around but my results are around generating a stacktrace by itself (using traceback) or preserving stacktraces (raise ... from ...), both are not what I am looking for.

Thank you.

0 Answers
Related