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

Global "except" condition

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.