2008-10-10 13:39:08

PyException P-p-p-p-panic

Python as a programming language features exceptions, as most modern languages do nowadays. However, since it is Python, these exceptions of course have to be named slightly differently from the way it is done everywhere else.

Usually, exceptions are thrown using throw new Exception(); and the construction to catch them is usually:

try {
    RunSomeCode();
}
catch (SomeException se)
{
    HandleErrorInSomeWay(se);
}

In Python, the construct does not deviate all that much logically, but the terminology is entirely different. Instead of throwing exceptions, Python raises them like signals (raise SomeException()). Also, they are not caught but excepted:

try:
    RunSomeCode()
except SomeException, se:
    HandleErrorInSomeWay(se)

Funnily, the indentation varies between the version. If a finally is added in Python 2.4, it cannot coexist in the same indentation level with except:

>>> try:
...     raise Exception()
... except:
...     print('Ugh')
... finally:
File "<stdin>", line 5
 finally:
SyntaxError: invalid syntax

This is of course fixed in Python 2.5, but since large amounts of wide-spread Python applications such as Zope still depend on Python 2.4, compatibility is a rather delicate problem here.

Moreover, however, exceptions don't seem to work stably even in the latest versions of Python. I have experienced various cases where Python would simply segfault when an exception is thrown:

$ python __init__.py
2008-10-10 13:34:25,572│__init__.py[37]: INFO: Initializing communication server
Segmentation fault (core dumped)
$

The corresponding code:

log.info("Initializing communication server")
try:
    (some code)
except Exception, e: # This is where the core dump occurrs
    log.critical('unable to unregister original servant')

One has to wonder how to handle this case from within Python; probably not at all. All one can do is to guess what the problem was and try to fix it.


Posted by Tonnerre Lombard | Permanent link | File under: broken