[Zope-dev] urgent: string exceptions

Guido van Rossum guido@python.org
Fri, 11 Apr 2003 11:18:32 -0400


> Chris McDonough wrote:
> > You can catch most string exceptions. 

[Lennart]
> I hate it when this happens. I thought you could, so I tried it, and it 
> didn't work. Now you say that is should work, so I try it again, and it 
> does... So stop telling my Python interpreter that it can do things it 
> couldn't before, I get all confused. :p
> 
>  > "Bad Request" cannot be caught because (afaik) the string isn't
>  > interned because it has a space in it.
> 
> Strangely enough, I now succeed in cathing 'Bad Request' exceptions when 
> I try it, so there is something more to this that my Zen-level doesn't grok.

Depends on how you try it.  If in one function you have

    raise "Bad Request"

and in another you have

    try:
        ...
    except "Bad Request":
        ...

it will fail.  OTOH if you have this in the same function, it will
succeed, because the bytecode compiler collapses identical constants
within one function even if they are not interned.

--Guido van Rossum (home page: http://www.python.org/~guido/)