Register multiple excepthooks?

Albert-Jan Roskam sjeik_appie at hotmail.com
Thu Aug 4 03:41:00 EDT 2022


   On Aug 1, 2022 19:34, Dieter Maurer <dieter at handshake.de> wrote:

     Albert-Jan Roskam wrote at 2022-7-31 11:39 +0200:
     >   I have a function init_logging.log_uncaught_errors() that I use for
     >   sys.excepthook. Now I also want to call another function
     (ffi.dlclose())
     >   upon abnormal termination. Is it possible to register multiple
     >   excepthooks, like with atexit.register? Or should I rename/redefine
     >   log_uncaught_errors() so it does both things?

     `sys.excepthook` is a single function (not a list of them).
     This means: at any moment a single `excepthook` is effective.

     If you need a modular design, use a dispatcher function
     as your `excepthook` associated with a registry (e.g. a `list`).
     The dispatcher can then call all registered function.

   ====
   Thank you both. I'll give this a try. I think it would be nice if the
   standard library function atexit.register would be improved, such that the
   registered functions would not only be called upon (a) normal program
   termination, but that one could also register functions that are called
   (b) upon error (c) unconditionally. Much like (a) try - (b) except - (c)
   finally.


More information about the Python-list mailing list