By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,755 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

Catching a traceback

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
EAS wrote:
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
Jul 18 '05 #2

P: n/a
>>>>> "Peter" == Peter Hansen <pe***@engcorp.com> writes:

Peter> EAS wrote:
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?


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.
Jul 18 '05 #3

P: n/a
Isaac To wrote:
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
Jul 18 '05 #4

P: n/a
EAS <er****@attbi.nospam.com> wrote:
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
Jul 18 '05 #5

P: n/a
Michael Geary wrote:
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
Jul 18 '05 #6

P: n/a
> 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


Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.