Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Wrapping exception handling around a function

Reply
Thread Tools

Wrapping exception handling around a function

 
 
Edward C. Jones
Guest
Posts: n/a
 
      02-12-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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-12-2004
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



 
Reply With Quote
 
 
 
 
Hans Nowak
Guest
Posts: n/a
 
      02-12-2004
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 ()
http://zephyrfalcon.org/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does Ruby support exception wrapping (exception chaining)? Hartin, Brian Ruby 15 02-23-2011 07:02 AM
MS work around on text wrapping in a datagrid does not work TB ASP .Net 2 02-22-2006 10:34 PM
Control text wrapping around boxes Marc HTML 1 01-18-2005 09:04 AM
Text wrapping around image my-wings HTML 3 06-05-2004 05:09 PM
Wrapping text around Image map problem Paul Kissock HTML 1 02-05-2004 09:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57