[Zope] uncatchable 'Bad Request' exceptions ?

Dieter Maurer dieter@handshake.de
Thu, 23 Aug 2001 21:23:42 +0200 (CEST)


Florent Guillaume writes:
 > Zope 2.3.3, python 1.5.2.
 > 
 > >From a python script I can't seem to catch a 'Bad Request' exception.
 > 
 > This doesn't work:
 > # script1
 > raise 'Bad Request', 'foo'
 > 
 > # script2
 > try: context.script1()
 > except 'Bad Request', x: return x
 > return 'nothing'
 > 
 > I get a Zope error:
 >  Error Type: Bad Request
 >  Error Value: foo
Python distinguishes between string based exceptions and
class based exceptions.

For string based exceptions, it uses object identity ("is") tests
to compare the exception type against the "except" parameter.
Usually, two strings are different objects, even if they
have the same content.
There are a few exceptions:

  *  string literals with identical content in the same source file
     are identical objects

  *  interned strings may yield identical objects

In your case above, the "Bad Request" in "script1" is not
identical to that in "script2".

 > But the following one works...
 > # script1
 > raise 'bar', 'foo'
 > 
 > # script2
 > try: context.script1()
 > except 'bar', x: return x
 > return 'nothing'
Probably, "bar" is interned...

 > And this one too...
 > # script1
 > raise 'Bad Request', 'foo'
 > 
 > # script2
 > try: context.script1()
 > except: return 'something'
 > return 'nothing'
Sure, as you catch all exceptions...

 > Anyone has an idea ? The exception is raised by another product which I
 > don't want to touch (CMF RegistrationTool), and I think I should be able
 > to catch them !
You should use the parameter-less "except" (last of your versions)
and check the exception type yourself.
For this, you will need to learn how to make "sys.exc_info"
available for Python scripts.
Recently, there was a nice description in the mailing list,
how to do that easily (--> mailing list archives).


Dieter