Thomas Dybdahl Ahle <lo****@gmail.comwrote:
Hi, I have a function, which looks like the following:
connecting = False
def func ():
global connecting
connecting = True
try:
# Do lot of network stuff
except Exception, e:
connecting = False
raise e
This works quite good, but it is a hell to debug. Instead of getting a
log message to the line which originally raised the exception.
Is there anyway to reraise an exception, but preserve the log?
Just use
raise
without any argument.
E.g.:
>>def raiser():
.... print 1/0
....
>>def reraise1():
.... print 'before'
.... try: raiser()
.... except Exception, e:
.... print 'after'
.... raise e
....
>>def reraise2():
.... print 'before'
.... try: raiser()
.... except Exception, e:
.... print 'after'
.... raise
....
>>reraise1()
before
after
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in reraise1
ZeroDivisionError: integer division or modulo by zero
>>reraise2()
before
after
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in reraise2
File "<stdin>", line 2, in raiser
ZeroDivisionError: integer division or modulo by zero
>>>
As you see, the traceback is "maintained" when you just "raise", though
it's altered when you "raise e".
Alex