5

I have a retry-mechanism in my python code. I want to raise an exception if all tries failed somehow

something like this:

last_exc = None
for i in range(3):
    try:
        raise Exception(i)
    except Exception as e:
        last_exc = e        
else:
    raise last_exc

But the issue is that I do not get the exact traceback in the logs. I just get the following message:

Traceback (most recent call last):
  File "snippet.py", line 8, in <module>
    raise e
Exception: 2

I was expecting raise Exception(i) (line:4) as the traceback in the exception. the behaviour is only with python 2.7.

How can I set the exact traceback of the exception I am raising, which was raised in the last exception?

Pratik
  • 1,351
  • 1
  • 20
  • 37
Jay Joshi
  • 1,402
  • 1
  • 13
  • 32
  • So my friend suggested a hack to python 2&3 compatible code. Just reduce the retry with 1 and put the last execution without a try - catch.! I can't believe this works. – Jay Joshi Nov 28 '19 at 10:55

0 Answers0