Catching a traceback

E

EAS

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.
 
P

Peter Hansen

EAS said:
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?

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
 
I

Isaac To

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.
 
P

Peter Hansen

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

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
 
M

Michael Geary

EAS said:
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?

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.MessageBox( log.text, 'Error' )

-Mike
 
P

Peter Hansen

Michael said:
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.MessageBox( log.text, 'Error' )

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

except:
tb = traceback.format_exception(*sys.exc_info())
win32ui.MessageBox(''.join(tb), 'Error')

-Peter
 
T

Terry Reedy

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

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
 

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
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top