Global "except" condition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ernesto

    #1

    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 anyExceptionOcc urs():
    if myCondition:
    print "Here's my global exception message"


    Is this functionality possible in python? If so, how?

  • Simon Forman

    #2
    Re: Global "except&qu ot; condition

    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 anyExceptionOcc urs():
    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

    Comment

    • Steve Holden

      #3
      Re: Global "except&qu ot; condition

      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 anyExceptionOcc urs():
      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

      Comment

      • Fredrik Lundh

        #4
        Re: Global "except&qu ot; condition

        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_exce ption(function) :
        def trampoline(*arg s, **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_exc eption
        def myfunc(x):
        return 1 / x

        @handle_any_exc eption
        def myotherfunc(fil ename):
        return open(filename)

        class MyClass:
        @handle_any_exc eption
        def mymethod(self):
        raise ValueError("oop s")

        myfunc(1)
        myfunc(0)
        myotherfunc("he llo.txt")
        MyClass().mymet hod()

        </F>



        Comment

        Working...