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

Wrapping exception handling around a function

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


P: n/a
Edward C. Jones wrote:
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

Jul 18 '05 #2

P: n/a
Edward C. Jones wrote:
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,

--
Hans (ha**@zephyrfalcon.org)
http://zephyrfalcon.org/

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.