En Wed, 10 Dec 2008 16:59:16 -0200, brooklineTom <[EMAIL PROTECTED]> escribió:

I want my exception handler to report the method that originally
raised an exception, at the deepest level in the call-tree. Let give
an example.

That's the default behavior, you don't have to do anything special.

import sys, traceback
class SomeClass:
    def error(self):
        """Raises an AttributeError exception."""
        int(3).zork()

    def perform_(self, aSelector):
        try:
            aMethod = getattr(self, aSelector, None)
            answer = apply(aMethod, [], {})
        except: AttributeError, anAttributeErrorException:
            aRawStack = traceback.extract_stack()
            answer = None

(I assume you're using Python < 3.0)
Use the 3-names form of the except statement:

        try:
            aMethod = getattr(self, aSelector, None)
            answer = aMethod()
        except AttributeError, e, tb:
            # the tb variable holds the traceback up to the error
            # the same thing you see printed by Python when
            # an unhandled error happens
            answer = None


Alternatively, you can obtain the same thing with sys.exc_info()[2]
Remember to delete any reference to the traceback object as soon as you're done with it; see http://docs.python.org/library/sys.html#sys.exc_info

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to