Exception passing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Dybdahl Ahle

    #1

    Exception passing

    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?
  • kyosohma@gmail.com

    #2
    Re: Exception passing

    On Mar 23, 9:29 am, Thomas Dybdahl Ahle <lob...@gmail.c omwrote:
    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

    Comment

    • Alex Martelli

      #3
      Re: Exception passing

      Thomas Dybdahl Ahle <lobais@gmail.c omwrote:
      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
      ZeroDivisionErr or: 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
      ZeroDivisionErr or: 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

      Comment

      • Thomas Dybdahl Ahle

        #4
        Re: Exception passing

        Den Fri, 23 Mar 2007 07:38:47 -0700 skrev Alex Martelli:
        Thomas Dybdahl Ahle <lobais@gmail.c omwrote:
        >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

        Comment

        Working...