Wrapping exception handling around a function

Edward C. Jones edcjones at erols.com
Thu Feb 12 09:45:20 EST 2004


Is there a better way to write the following?

--------

import sys, traceback

class LocalError(StandardError):
     pass

_first = True

# "fcn(*args)" is executed. If one of the exceptions listed in
# "catchtuple" is thrown in "fcn", the exception is caught here.
# The traceback and message are written to "badfile". Then
# "LocalError" is raised. "wrapper" is useful when testing a
# collection of functions.
def wrapper(badfile, fcn, args, catchtuple):
     global _first
     try:
         fcn(*args)
     except tuple(catchtuple), message:
         if _first:
             bad = file(badfile, 'w')
             _first = False
         traceback.print_exc(100, bad)
         raise LocalError



More information about the Python-list mailing list