Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7735601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:32:32+00:00 2026-06-01T07:32:32+00:00

Say I have something like this, which sends unhanded exceptions to logging.critical() : import

  • 0

Say I have something like this, which sends unhanded exceptions to logging.critical():

import sys

def register_handler():
    orig_excepthook = sys.excepthook

    def error_catcher(*exc_info):
        import logging
        log = logging.getLogger(__name__)
        log.critical("Unhandled exception", exc_info=exc_info)
        orig_excepthook(*exc_info)

    sys.excepthook = error_catcher

It works:

import logging
logging.basicConfig()

register_handler()

undefined() # logs, then runs original excepthook

However if register_handler() is called multiple times, multiple error_catcher‘s are called in a chain, and the logging message appears several times..

I can think of a few ways, but none of them are particularly good (like checking if sys.excepthook is the error_catcher function, or using a “have_registered” attribute on the module to avoid double-registering)

Is there a recommended way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:32:33+00:00Added an answer on June 1, 2026 at 7:32 am

    Having a module-level “have the hook already been registered” variable seems like the simplest and most reliable way of doing this.

    The other possible solutions would fall over in certain (rather obscure) circumstances –
    checking if the sys.excepthook is a builtin function will fail if an application registers a custom excepthook, storing the original excepthook at function-definition time will clobber subsequently registered excepthook functions.

    import sys
    
    _hook_registered = False
    
    def register_handler(force = False):
        global _hook_registered
    
        if _hook_registered and not force:
            return
    
        orig_excepthook = sys.excepthook
    
        def error_catcher(*exc_info):
            import logging
            log = logging.getLogger(__name__)
            log.critical("Unhandled exception", exc_info=exc_info)
            orig_excepthook(*exc_info)
    
        sys.excepthook = error_catcher
    
        _hook_registered = True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say I have something like this: id | title | 1 | First
Let's say I have something like this: $db=new PDO($dsn); $statement=$db->query('Select * from foo'); while
Let's say I started a thread and I have something like this: ...//initiate all
Let's say I have a MySQL table that is something like this: software table:
Let's say I have a list of words, something like this: ['The', 'Quick', 'Brown',
Let's say I have SELECT name FROM table which gives me something like foo
Let's say I have a grammar defined to something like: some_rule := a b
Let's say I have a bunch of classes that look something like... class Foo{
Possible Duplicate: C printing bits Lets say I have a uint16_t number something like:
First, please note, that I am interested in how something like this would work,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.