Catching a traceback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • EAS

    Catching a traceback

    Hi,

    I'm wondering if there is any way to keep a program running when it runs
    into an error (using the 'try' structure) and print the traceback to the
    screen?

    Thanks.


  • Peter Hansen

    #2
    Re: Catching a traceback

    EAS wrote:
    [color=blue]
    > I'm wondering if there is any way to keep a program running when it runs
    > into an error (using the 'try' structure) and print the traceback to the
    > screen?[/color]

    I'm guessing about what you want, because it's not really
    clear. If I'm right about what you want, it's not possible.
    Basically, once an exception is raised by the failing code,
    the code that was executing cannot be continued (as I
    think you mean by "keep a program running").

    If the exception was raised in very low level code, but was
    not caught by any 'except' statement, the program will
    terminate with an error message, as you no doubt know.
    Even if you put a "default" try/except around the very
    top of the program, all you are doing is catching the
    exception just before it exits. If the rest of the
    program was not designed to restart in any way, that's
    it. It's done. No "keep it running".

    You can, of course, catch any exception at any level and
    print tracebacks. In fact, if this is really what you
    were focusing on, then the answer to your question might be
    "yes". If you can write the code so that it does catch
    an exception at the right place to allow safe continuation,
    then you can certainly keep your program running, and many
    people use exceptions in exactly that way.

    -Peter

    Comment

    • Isaac To

      #3
      Re: Catching a traceback

      >>>>> "Peter" == Peter Hansen <peter@engcorp. com> writes:

      Peter> EAS wrote:[color=blue][color=green]
      >> I'm wondering if there is any way to keep a program running when it
      >> runs into an error (using the 'try' structure) and print the
      >> traceback to the screen?[/color][/color]

      Peter> You can, of course, catch any exception at any level and print
      Peter> tracebacks. In fact, if this is really what you were focusing
      Peter> on, then the answer to your question might be "yes". If you can
      Peter> write the code so that it does catch an exception at the right
      Peter> place to allow safe continuation, then you can certainly keep
      Peter> your program running, and many people use exceptions in exactly
      Peter> that way.

      I think the OP's query is mainly about whether it is possible to print a
      stack trace without crashing the whole program.

      Regards,
      Isaac.

      Comment

      • Peter Hansen

        #4
        Re: Catching a traceback

        Isaac To wrote:
        [color=blue]
        > I think the OP's query is mainly about whether it is possible to print a
        > stack trace without crashing the whole program.[/color]

        If that's the case, then he should check the documentation
        for the "traceback" module... it has the facilities for
        doing this easily, sometimes in conjunction with sys.exc_info().

        -Peter

        Comment

        • Michael Geary

          #5
          Re: Catching a traceback

          EAS <eriksp@attbi.n ospam.com> wrote:[color=blue]
          > I'm wondering if there is any way to keep a program running
          > when it runs into an error (using the 'try' structure) and print
          > the traceback to the screen?[/color]

          Here's an example of something I did that was similar to what it sounds like
          you're looking for. I was writing a Windows application using native Win32
          calls, and I wanted to catch any errors and display them in a message box
          instead of to the console (because there was no console). In this case, I
          just let the program terminate after the message box, but you can take it
          from there.

          In this example, I've replaced the original main() function with one that
          always raises an error:

          import traceback, win32ui

          def main():
          print unknown

          class TraceLog:
          def __init__( self ):
          self.text = ''

          def write( self, text ):
          self.text += text

          try:
          main()
          except:
          log = TraceLog()
          traceback.print _exc( file=log )
          win32ui.Message Box( log.text, 'Error' )

          -Mike


          Comment

          • Peter Hansen

            #6
            Re: Catching a traceback

            Michael Geary wrote:
            [color=blue]
            > import traceback, win32ui
            >
            > def main():
            > print unknown
            >
            > class TraceLog:
            > def __init__( self ):
            > self.text = ''
            >
            > def write( self, text ):
            > self.text += text
            >
            > try:
            > main()
            > except:
            > log = TraceLog()
            > traceback.print _exc( file=log )
            > win32ui.Message Box( log.text, 'Error' )[/color]

            Simpler to use format_exceptio n() and not do all that stuff
            with TraceLog, unless you already had it around for something
            else:

            except:
            tb = traceback.forma t_exception(*sy s.exc_info())
            win32ui.Message Box(''.join(tb) , 'Error')

            -Peter

            Comment

            • Terry Reedy

              #7
              Re: Catching a traceback

              > def write( self, text ):[color=blue]
              > self.text += text[/color]

              With strings, += n times takes O(n**2) time, whereas n appends and join is
              O(n). While ok to use when one knows for sure that n will be small, this
              strike me as a bad pattern to post publicly, without comment, where new
              Pythoneers might get the wrong idea (as evidenced by some past postings
              ;-). For tracebacks, n is usually definitely small but the occasional n up
              to maybe 50 might not be considered so.

              Terry J. Reedy




              Comment

              Working...