469,927 Members | 1,394 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

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 1962
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Raymond Arthur St. Marie II of III | last post: by
10 posts views Thread by Derek Basch | last post: by
27 posts views Thread by Ron Adam | last post: by
4 posts views Thread by Gestorm | last post: by
24 posts views Thread by Jeremy J Starcher | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.