473,511 Members | 14,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrapping exception handling around a function

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
2 1592
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3822
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
6
2322
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
3
2050
by: Monster | last post by:
Are they expensive because of what happens when an exception is thrown or because of the extra checking involved when you wrap exceptions around code?
3
2305
by: __PPS__ | last post by:
Hi, I've read in documentation to different libraries that their exception classes aren't subclasses from std::exception, and a separate catch statement is required for their exceptions. Always, I...
10
2350
by: Tom the Canuck | last post by:
What would be the best way to proceed? Should I make a pure virtual class and then derive from that? I want the base class to have functions defined so that I don't have to do the work all over...
6
1171
by: pigeonrandle | last post by:
Hi, I have written several pieces of software and would like to hear any thoughts anyone has on what measures i should take to ensure(!) its stability and security at the hands of the *users*. I...
6
3384
by: Phillip Taylor | last post by:
I don't like the way every time I write a bad sql query the debugger takes me to the database access code. It's extremely stable and reliable and the exceptions being thrown are because of SQL...
1
3089
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
35
3469
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.