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 583065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:45:12+00:00 2026-05-13T14:45:12+00:00

I wanted to handle all internal errors gracefully, without program termination. As discussed here

  • 0

I wanted to handle all internal errors gracefully, without program termination.

As discussed here, using _set_se_translator catches divide-by-zero errors.

But it does not catch, for example, C runtime library error -1073740777 (0xc0000417), which can be caused by format strings for printf which have the percent sign where they shouldn’t. (That is just an example; of course we should check for such strings). To handle these, _set_invalid_parameter_handler is needed.

There are about ten other such handlers listed here.

In addition, this one will catch uncaught C++ exceptions: SetUnhandledExceptionFilter. Thus, it can be used together with the __set__ … functions. (An article about using it in MSVC 2008.)

I want to catch any and all errors so I can handle them (by logging, throwing a modern C++ standard exception and returning an application-specific error code). Is there a single handler that catches everything?

See also this on StackOverflow.

I am using Visual Studio 2008.

  • 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-05-13T14:45:13+00:00Added an answer on May 13, 2026 at 2:45 pm

    There is no universal handler. You need to install each one. I have used something like this:

    ///////////////////////////////////////////////////////////////////////////
    template<class K, class V>
    class MapInitializer
    {
        std::map<K,V> m;
    public:
        operator std::map<K,V>() const 
        { 
            return m; 
        }
    
        MapInitializer& Add( const K& k, const V& v )
        {
            m[ k ] = v;
            return *this;
        }
    };
    
    ///////////////////////////////////////////////////////////////////////////
    struct StructuredException : std::exception
    {
        const char *const msg;
        StructuredException( const char* const msg_ ) : msg( msg_ ) {}
        virtual const char* what() const { return msg; }
    };
    
    ///////////////////////////////////////////////////////////////////////////
    class ExceptionHandlerInstaller
    {
    public:
        ExceptionHandlerInstaller()
            : m_oldTerminateHandler( std::set_terminate( TerminateHandler ) )
            , m_oldUnexpectedHandler( std::set_unexpected( UnexpectedHandler ) )
            , m_oldSEHandler( _set_se_translator( SEHandler ) )
        {}
    
        ~ExceptionHandlerInstaller() 
        { 
            std::set_terminate( m_oldTerminateHandler );
            std::set_unexpected( m_oldUnexpectedHandler );
            _set_se_translator( m_oldSEHandler );
        }
    
    private:
        static void TerminateHandler() 
        { 
            TRACE( "\n\n**** terminate handler called! ****\n\n" );
        }
    
        static void UnexpectedHandler() 
        { 
            TRACE( "\n\n**** unexpected exception handler called! ****\n\n" );
        }
    
        static void SEHandler( const unsigned code, EXCEPTION_POINTERS* )
        {
            SEMsgMap::const_iterator it = m_seMsgMap.find( code );
            throw StructuredException( it != m_seMsgMap.end() 
                ? it->second 
                : "Structured exception translated to C++ exception." );
        }
    
        const std::terminate_handler  m_oldTerminateHandler;
        const std::unexpected_handler m_oldUnexpectedHandler;
        const _se_translator_function m_oldSEHandler;
    
        typedef std::map<unsigned, const char*> SEMsgMap;
        static const SEMsgMap m_seMsgMap;
    };
    
    ///////////////////////////////////////////////////////////////////////////
    // Message map for structured exceptions copied from the MS help file
    ///////////////////////////////////////////////////////////////////////////
    const ExceptionHandlerInstaller::SEMsgMap ExceptionHandlerInstaller::m_seMsgMap 
        = MapInitializer<ExceptionHandlerInstaller::SEMsgMap::key_type, 
                         ExceptionHandlerInstaller::SEMsgMap::mapped_type>()
        .Add( EXCEPTION_ACCESS_VIOLATION,         "The thread attempts to read from or write to a virtual address for which it does not have access. This value is defined as STATUS_ACCESS_VIOLATION." )
        .Add( EXCEPTION_ARRAY_BOUNDS_EXCEEDED,    "The thread attempts to access an array element that is out of bounds, and the underlying hardware supports bounds checking. This value is defined as STATUS_ARRAY_BOUNDS_EXCEEDED." )
        .Add( EXCEPTION_BREAKPOINT,               "A breakpoint is encountered. This value is defined as STATUS_BREAKPOINT." )
        .Add( EXCEPTION_DATATYPE_MISALIGNMENT,    "The thread attempts to read or write data that is misaligned on hardware that does not provide alignment. For example, 16-bit values must be aligned on 2-byte boundaries, 32-bit values on 4-byte boundaries, and so on. This value is defined as STATUS_DATATYPE_MISALIGNMENT." )
        .Add( EXCEPTION_FLT_DENORMAL_OPERAND,     "One of the operands in a floating point operation is denormal. A denormal value is one that is too small to represent as a standard floating point value. This value is defined as STATUS_FLOAT_DENORMAL_OPERAND." )
        .Add( EXCEPTION_FLT_DIVIDE_BY_ZERO,       "The thread attempts to divide a floating point value by a floating point divisor of 0 (zero). This value is defined as STATUS_FLOAT_DIVIDE_BY_ZERO." )
        .Add( EXCEPTION_FLT_INEXACT_RESULT,       "The result of a floating point operation cannot be represented exactly as a decimal fraction. This value is defined as STATUS_FLOAT_INEXACT_RESULT." )
        .Add( EXCEPTION_FLT_INVALID_OPERATION,    "A floatin point exception that is not included in this list. This value is defined as STATUS_FLOAT_INVALID_OPERATION." )
        .Add( EXCEPTION_FLT_OVERFLOW,             "The exponent of a floating point operation is greater than the magnitude allowed by the corresponding type. This value is defined as STATUS_FLOAT_OVERFLOW." )
        .Add( EXCEPTION_FLT_STACK_CHECK,          "The stack has overflowed or underflowed, because of a floating point operation. This value is defined as STATUS_FLOAT_STACK_CHECK." )
        .Add( EXCEPTION_FLT_UNDERFLOW,            "The exponent of a floating point operation is less than the magnitude allowed by the corresponding type. This value is defined as STATUS_FLOAT_UNDERFLOW." )
        .Add( EXCEPTION_GUARD_PAGE,               "The thread accessed memory allocated with the PAGE_GUARD modifier. This value is defined as STATUS_GUARD_PAGE_VIOLATION." )
        .Add( EXCEPTION_ILLEGAL_INSTRUCTION,      "The thread tries to execute an invalid instruction. This value is defined as STATUS_ILLEGAL_INSTRUCTION." )
        .Add( EXCEPTION_IN_PAGE_ERROR,            "The thread tries to access a page that is not present, and the system is unable to load the page. For example, this exception might occur if a network connection is lost while running a program over a network. This value is defined as STATUS_IN_PAGE_ERROR." )
        .Add( EXCEPTION_INT_DIVIDE_BY_ZERO,       "The thread attempts to divide an integer value by an integer divisor of 0 (zero). This value is defined as STATUS_INTEGER_DIVIDE_BY_ZERO." )
        .Add( EXCEPTION_INT_OVERFLOW,             "The result of an integer operation causes a carry out of the most significant bit of the result. This value is defined as STATUS_INTEGER_OVERFLOW." )
        .Add( EXCEPTION_INVALID_DISPOSITION,      "An exception handler returns an invalid disposition to the exception dispatcher. Programmers using a high-level language such as C should never encounter this exception. This value is defined as STATUS_INVALID_DISPOSITION." )
        .Add( EXCEPTION_INVALID_HANDLE,           "The thread used a handle to a kernel object that was invalid (probably because it had been closed.) This value is defined as STATUS_INVALID_HANDLE." )
        .Add( EXCEPTION_NONCONTINUABLE_EXCEPTION, "The thread attempts to continue execution after a non-continuable exception occurs. This value is defined as STATUS_NONCONTINUABLE_EXCEPTION." )
        .Add( EXCEPTION_PRIV_INSTRUCTION,         "The thread attempts to execute an instruction with an operation that is not allowed in the current computer mode. This value is defined as STATUS_PRIVILEGED_INSTRUCTION." )
        .Add( EXCEPTION_SINGLE_STEP,              "A trace trap or other single instruction mechanism signals that one instruction is executed. This value is defined as STATUS_SINGLE_STEP." )
        .Add( EXCEPTION_STACK_OVERFLOW,           "The thread uses up its stack. This value is defined as STATUS_STACK_OVERFLOW." );
    

    Then in main or app init, I do this:

    BOOL CMyApp::InitInstance()
    {
        ExceptionHandlerInstaller ehi;
        // ...
    }
    

    Note that this translates structured exceptions to regular exceptions but handles terminate (which gets called, e.g., when a nothrow() function throws an exception) by simply printing an error message. It is highly unlikely that you want to use a single handler for all different types of errors, which is why they don’t provide it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 396k
  • Answers 396k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I figured it out. I was using the decimal values… May 15, 2026 at 2:52 am
  • Editorial Team
    Editorial Team added an answer How many computer programming languages are used all in the… May 15, 2026 at 2:52 am
  • Editorial Team
    Editorial Team added an answer According to the Http Client tutorial, you can do this:… May 15, 2026 at 2:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.