Detecting an active exception
Nick Craig-Wood
nick at craig-wood.com
Sun Jun 3 15:30:05 EDT 2007
NeBlackCat <lists at asd-group.com> wrote:
> I've now also looked at this from a traceback and stack frame walking
> perspective as well and it seems impossible, ie. it see,s impossible to
> create a function AmIHandlingAnException() which would behave as follows:
>
> try:
> a=a+1
> except:
> pass
>
> try:
> AmIHandlingAnException() # returns FALSE
> b = b + 1
> except:
> AmIHandlingAnException() # returns TRUE
> try:
> AmIHandlingAnException() # returns TRUE
> except:
> pass
>
> AmIHandlingAnException() # returns FALSE
>
> The only way I can see to do it is to use something like the 'inspect'
> module to walk up the stack and, for each frame, walk lines of code
> backwards to determine whether or not you're in an except: block. Of
> course that would be horribly inefficient.
>
> I'm rather stunned by this (this was the 'easy' bit that I left to
> last!).
Why don't you tell us what you are actually trying to achieve and we
can see if we can come up with a more pythonic solution? The fact
that you are running into limits of the language like this as a new
python programmer probably means you aren't thinking in python yet.
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list