Wrapping exception handling around a function

E

Edward C. Jones

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
 
P

Peter Otten

Edward said:
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

I find, e. g.

wrapper("logfile.txt", file, ["somefile.txt", "w"], (IOError,))

much less readable than

import logging

logger = logging.getLogger("mylogger")

try:
f = file("somefile.txt", "w")
except IOError:
logger.exception("some hints")
raise LocalError("some hints")

and don't mind the few extra keystrokes. As for recording only the first
traceback, I'm not that familiar with the logging module, but I still
recommend it over a selfmade scheme.

Peter
 
H

Hans Nowak

Edward said:
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

Hm. What happens if you call this function for the second time? _first will
be false, and 'bad' will not be set.

It appears that all you want is to log the exception to a file... create it if
it doesn't exist, append to it otherwise. So you might be better off doing
something like

except tuple(catchtuple), message:
bad = file(badfile, 'a+')
traceback.print_exc(100, bad)
bad.close()
raise LocalError

HTH,
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top