exception question

Gonçalo Rodrigues op73418 at mail.telepac.pt
Mon Sep 1 01:23:43 CEST 2003

On Mon, 01 Sep 2003 00:14:13 +0200, Peter Otten <__peter__ at web.de>

>Gonçalo Rodrigues wrote:
>> For error processing I found convenient maintaining a dictionary where
>> the keys are exception *classes* and the values are callables. Of
>> course, for this to work, exception classes have to be hashable which
>> I happily found that they were. So my question is, can I count on this
>> behaviour? Or is this behaviour I should not count on? (I found
>> nothing on the docs about it, thus the question).
>(No answer to your question)
>import sys
>class MyException(Exception):
>    def __init__(self, msg, handler):
>        Exception.__init__(self, msg)
>        self.handler = handler
>    raise MyException("yup", lambda: sys.stdout.write("call it sleep\n"))
>except MyException, e:
>    e.handler()
>Would that eliminate the need for a dictionary?

Yup, I did though of of a scheme like that before -- It's my fall back
solution in case I should not count on the fact that exception classes
are hashable.

Thanks anyway,
G. Rodrigues

More information about the Python-list mailing list