C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::typeid_except ( Paper Attached )

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 3 Mar 2024 22:15:40 +0000
On Sun, Mar 3, 2024 at 6:04 PM Tom Honermann wrote:
>
> Microsoft's C++ exception handling implementation is built on top of SEH
> exceptions. When compiled in cl /EHa mode, a catch(...) handler will be
> entered for SEH exceptions. In this case, there isn't really an
> associated C++ type for the exception. See
> https://learn.microsoft.com/en-us/cpp/cpp/exception-handling-differences?view=msvc-170.
> This is obviously implementation-defined territory, but it might be
> worth exploring some. I don't know what std::current_exception() returns
> when called in these cases and it isn't convenient for me to check right
> now. I'd be curious to learn what is done though.


If you scroll down to the end of that webpage you linked us to, take a
look at the last block of code. I've modified it so that SEH_Exception
derives from std::exception as follows:

    // Compile with Microsoft C++ compiler with option /EHa
    #include <cstdio> // sprintf
    #include <exception> // exception
    #include <iostream> // cout, endl
    #include <eh.h> // _set_se_translator, _EXCEPTION_POINTERS

    class SEH_Exception : public std::exception {
    private:
    unsigned const var_number;
    mutable char var_what[14u + 1u];
    public:
        SEH_Exception(unsigned const arg) noexcept : var_number(arg) {}
        unsigned number(void) noexcept { return var_number; }
        char const *what(void) const noexcept override
        {
            std::sprintf(this->var_what, "SEH 0x%08x", this->var_number );
            return this->var_what;
        }
    };

    void DivideByZero(void)
    {
        int x, y = 0;
        x = 5 / y;
    }

    void trans_func(unsigned const u, _EXCEPTION_POINTERS*)
    {
        throw SEH_Exception(u);
    }

    int main(void)
    {
        _set_se_translator( trans_func );
        try
        {
            DivideByZero();
        }
        catch(std::exception const &e)
        {
            std::cout << e.what() << std::endl;
        }
    }

This is one way that Microsoft could do it. Alternatively they could
throw a simple 'unsigned int', or an SEH_Exception class that doesn't
derive from std::exception.

Received on 2024-03-03 22:15:52