I’m adding logging to some python code that deals with exceptions, in the example below what’s the correct syntax for wanting to log exception details (e.g. via logger.exception()) when a TypeError or AttributeError occurs?
try:
...
except (TypeError, AttributeError):
# want to do a logger.exception(x) here but not sure what to use for x
...
raise CustomError("Unable to parse column status)
If you want the exception details, you need to bind the exception itself to a local variable, like this:
If you need to do different things based on the type of the exception, then you can catch them separately:
And if you need more information about the context of the exception itself, look into the
sys.exc_info()function; it can get you the traceback, and the details about exactly where the exception occurred.