Exception passing

2007-03-23 Thread 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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception passing

2007-03-23 Thread kyosohma
On Mar 23, 9:29 am, Thomas Dybdahl Ahle [EMAIL PROTECTED] wrote:
 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception passing

2007-03-23 Thread Alex Martelli
Thomas Dybdahl Ahle [EMAIL PROTECTED] wrote:

 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception passing

2007-03-23 Thread Thomas Dybdahl Ahle
Den Fri, 23 Mar 2007 07:38:47 -0700 skrev Alex Martelli:

 Thomas Dybdahl Ahle [EMAIL PROTECTED] wrote:

 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.

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

Thanks a lot :D
-- 
http://mail.python.org/mailman/listinfo/python-list