Catching a traceback

Peter Hansen peter at engcorp.com
Tue Jun 8 07:06:48 EDT 2004


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



More information about the Python-list mailing list