473,400 Members | 2,145 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,400 software developers and data experts.

Global "except" condition

Within the scope of one Python file (say myFile.py), I'd like to print
a message on ANY exception that occurs in THAT file, dependent on a
condition.

Here's the pseudocode:

if anyExceptionOccurs():
if myCondition:
print "Here's my global exception message"
Is this functionality possible in python? If so, how?

Jul 10 '06 #1
3 2161
Ernesto wrote:
Within the scope of one Python file (say myFile.py), I'd like to print
a message on ANY exception that occurs in THAT file, dependent on a
condition.

Here's the pseudocode:

if anyExceptionOccurs():
if myCondition:
print "Here's my global exception message"
Is this functionality possible in python? If so, how?
Either wrap all your module level code in a try:.. except:.. statement,
or you can replace the sys.excepthook() function with your own custom
function (see http://docs.python.org/lib/module-sys.html) but that
would then be called for all uncaught exceptions, even those that were
raised from modules other than your myFile.py.

HTH,
~Simon

Jul 11 '06 #2
Ernesto wrote:
Within the scope of one Python file (say myFile.py), I'd like to print
a message on ANY exception that occurs in THAT file, dependent on a
condition.

Here's the pseudocode:

if anyExceptionOccurs():
if myCondition:
print "Here's my global exception message"
Is this functionality possible in python? If so, how?
What if your module defines a function, and then that function is called
from some other module and raises an exception. Would you then want the
global exception message to appear?

If so then I'm not sure that what you want is possible.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 11 '06 #3
Ernesto wrote:
Within the scope of one Python file (say myFile.py), I'd like to print
a message on ANY exception that occurs in THAT file, dependent on a
condition.
condition = True

def handle_any_exception(function):
def trampoline(*args, **kwargs):
try:
return function(*args, **kwargs)
except:
if not condition:
raise
print "exception caught in", function.__name__
return "n/a" # default return value
return trampoline

@handle_any_exception
def myfunc(x):
return 1 / x

@handle_any_exception
def myotherfunc(filename):
return open(filename)

class MyClass:
@handle_any_exception
def mymethod(self):
raise ValueError("oops")

myfunc(1)
myfunc(0)
myotherfunc("hello.txt")
MyClass().mymethod()

</F>

Jul 11 '06 #4

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

Similar topics

0
by: Raymond Arthur St. Marie II of III | last post by:
Del's "except"ional PEP Rejection Ladieees and Gentilmen and Pyth-O-neers of all ages. Step right up. Don't be shy. Come one come all. Be the first on your block TO GROK *THE* one *THE* only...
10
by: Derek Basch | last post by:
Hello, I have a string like: string = "WHITE/CLARET/PINK/XL" which I need to alter to this format: string = "WHITE-CLARET-PINK/XL"
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
5
by: Nebur | last post by:
I'm using the contract.py library, running Python 2.4.4. Now I'm confronted with the following exception backtrace: (...) File "/usr/lib/python2.4/site-packages/contract.py", line 1265, in...
0
by: egur | last post by:
Hi, I'm looking for reliable MySQL version (for Windows) that supports "EXCEPT" and "INTERSECT" execution. Would somebody recommend such version (and corresponding client), please?
4
by: Gestorm | last post by:
Hi all, I found a macro "USE_VAR" in the code of bash-3.2 as follows: /*file: bash-3.2/shell.c*/ 344 USE_VAR(argc); 345 USE_VAR(argv); 346 USE_VAR(env); 347 USE_VAR(code); 348 ...
24
by: Jeremy J Starcher | last post by:
While reading c.l.j, I've noticed that some people prefer and indeed even recommend the use of "window.alert()" over "alert()". I can't find any technical reason to make this distinction, and...
4
by: GLSmyth | last post by:
I need to select cells from one table that do not appear in a second table. I know that this can be done in some flavors of SQL by using Except: Select Unit_PK From Table1 Except Select Unit_PK...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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.