473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Function to log exceptions and keep on truckin

import sys, traceback
def e2str(id):
"""Return a string with information about the current exception. id
is arbitrary string included in output."""

exc = sys.exc_info()
file, line, func, stmt = traceback.extract_tb(exc[2])[-1]
return("%s: %s line %s (%s): %s" % (id, func, line, repr(stmt),
str(exc[1])))
This function returns a string containing useful information about the
current exception.
It takes as argument an arbitrary string that gets included in the
output. You can include anything else that might be useful in this
string, such as a loop counter or other local variable.
You can use this function to log anything you'd want to know about an
exception but continue running.
For example:

for i in interable:
try:
some_function(i)
except NonFatalError:
print e2str(str(i))
For some reason I've been into closures lately, so I like to have the
following code in a general utility module, from which I import *:

def _make_e2str():
"""This function creates a closure e2str."""
import sys, traceback
def e2str(id):
"""Return a string with information about the current
exception. id is arbitrary string included in output."""

exc = sys.exc_info()
file, line, func, stmt = traceback.extract_tb(exc[2])[-1]
return("%s: %s line %s (%s): %s" % (id, func, line, repr(stmt),
str(exc[1])))
return e2str
e2str = _make_e2str()
del _make_e2str #clean up the namespace...

Jul 19 '05 #1
0 877

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: jimfortune | last post by:
I have an A97 module called modWorkdayFunctions in: http://www.oakland.edu/~fortune/WorkdayFunctions.zip It allows the counting of workdays taking into consideration up to 11 U.S. holidays. ...
10
by: Cool Guy | last post by:
Consider: void Start() { if (!TryToDoSomething()) ShowErrorMessage(); }
5
by: Gerard | last post by:
Hi, Is it an option/idea to make the exceptions thrown be part of the signature of a function? - So I'll get a warning if I call a function and not handle one of the exceptions. - So I'll get...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...
0
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...

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.