Re: arbitrary exception handlers

graham@laplace.anu.edu.au
Thu, 23 Feb 95 09:24:23 +1100

>I'm not sure I understand your problem. If you call code in another
>module from within a try-except statement, your exception handler will
>still be invoked, e.g.
>
> # module A
> def f(): raise RuntimeError
>
>and
>
> # module B
> import A
> try:
> A.f()
> except RuntimeError:
> print 'Got it'
>
>This will print 'Got it'.
>
>However, I feel that what you really want is to install an exception
>handler that gets called after the code that installed it is gone,
>like
>
> # module A
> def f():
> on RuntimeError: print 'Got it'
>
>and
>
> # module B
> import A
> A.f()
> raise RuntimeERror

This is I think what I am doing yes. My code does something like this,

W1 = start_some_widget
W2 = start_some_widget
...
Wn = start_some_widget

root.mainloop()

Some of these widgets respond to events, and this response code can
cause exceptions (for example some of the widgets do file saves). I
would like to catch all these exceptions using a single piece of
code in my "main" program. So set up something like,

exception catcher for E1, E2, ..., En
...

W1 = start_some_widget
W2 = start_some_widget
...
Wn = start_some_widget

root.mainloop()

But I suspect this is what you mean by installing an exception
handler that gets called "after the code that installed it is gone".

>This is not possible in Python. It smells of signal handling though
>-- maybe you can do something with the signal module?

What I have done is to pass each widget an error handler which the
widget calls on error, passing an exception, and args. Its not
pretty but its effective.

graham

Je suis pour le communisme
Je suis pour le socialisme
Je suis pour le capitalisme
Parce que je suis opportuniste