A Catch-all Exception-Handler

Michael P. Reilly arcege at shore.net
Fri May 7 08:44:53 EDT 1999


Fredrik Lundh <fredrik at pythonware.com> wrote:
: Norbert Klamann wrote:
:> since some weeks I do serious work with Python and I like it very much.
:> I wrote some scripts which run nightly in batch . At the moment I don't
:> see a possibility to make any unforeseen exceptions visible.
:> 
:> The scripts print to stdout and in case of an exceptional exception I would
:> like to see a traceback on Stdout.
:> 
:> If possible I wouldn't like to clutter the code with try's and except's at
:> every second line.

: import traceback, sys

: for stuff in things_to_do():
:     try:
:         do(stuff)
:         print stuff, "ok"
:     except:
:         print stuff, "FAILED"
:         traceback.print_exc(file=sys.stdout)

Or if you don't mind all stderr going to stdout, just:
  import sys
  sys.stderr = sys.stdout

The traceback module routines all use the current value of "sys.stderr"
at the time of the exception.

  -Arcege





More information about the Python-list mailing list