Exception passing

  • Thread starter Thomas Dybdahl Ahle
  • Start date
T

Thomas Dybdahl Ahle

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?
 
K

kyosohma

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?

You could import traceback and use its functionality.

Lundh mentioned using sys.exc_info about an hour ago to another user.
See http://effbot.org/pyref/sys.exc_info.htm

Mike
 
A

Alex Martelli

Thomas Dybdahl Ahle said:
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.:
.... print 1/0
.... .... print 'before'
.... try: raiser()
.... except Exception, e:
.... print 'after'
.... raise e
.... .... print 'before'
.... try: raiser()
.... except Exception, e:
.... print 'after'
.... raise
.... before
after
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
before
after
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in reraise2

As you see, the traceback is "maintained" when you just "raise", though
it's altered when you "raise e".


Alex
 
T

Thomas Dybdahl Ahle

Den Fri, 23 Mar 2007 07:38:47 -0700 skrev Alex Martelli:
As you see, the traceback is "maintained" when you just "raise", though
it's altered when you "raise e".

Thanks a lot :D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top