[Zope] Exception handling and Site Error Log
Dieter Maurer
dieter at handshake.de
Fri Apr 23 15:08:17 EDT 2004
John Ziniti wrote at 2004-4-23 11:19 -0400:
>> You definitely should not catch exceptions yourself.
>> You risk to get your databases (ZODB and relational databases)
>> in an inconsistent state unless you raise again an exception.
>
>Really? Is there any more detail to this than "just don't
>do it". I rely on the fact that code in External Methods
>and full-blown Products can catch exceptions and handle
>them gracefully (it's one of Python's best features!!). It
>would hurt a lot to have to stop using them.
Normal Python appliciations do not manipulate persistent data.
Therefore, Python's aborting exception handling may be adequate.
Whenever you handle persistent data, catching exceptions
(without raising this or another exception and without
explicitly aborting the transaction) gives you a good chance
to commit an inconsistent state.
Furthermore, you must keep in mind, that Zope/ZODB uses
exceptions as a way to communicate with ZPublisher -- via
"ConflictError" exceptions. When you turn them into another
exception, you made a harmless thing (the request would have been
retried) into an error. When you ignore them, part of the operation
was not executed without anyone noticing it.
You should only catch an exception when both of the following
conditions are met:
1. you know that the exception is not really exeptional but normal
behaviour.
2. you know that the code inside the "try: ... except XXX:"
has not modified persistent state (or you abort the transaction
explicitly)
Usually, you know this only for a few well defined exceptions.
Most exceptions are truely exceptional and must not be catched.
"ConflictError" definitely must not be catched.
--
Dieter
More information about the Zope
mailing list