***Say I want to reraise an exception with extra information for handling – what would be the best practice for that?
I came up with the following but it smells fishy to me:***
`def internal_function():
raise ValueError("smelly socks!")
def context_function():
try:
internal_function()
except Exception as e:
e.args= e.args[:1] + ("<raise_context>",) + e.args[1:] # raise context varies on conditions ofc
raise e
def handler_function():
try:
context_function()
except Exception as e:
msg, context, *other = e.args
if context == "some_context":
pass # handle one way
elif context == "some other context":
pass # handle another way`
wants to amend the message string rather than adding separate metadata.
New contributor
bodawy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.