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

  • Home
  • SEARCH
  • 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 7549805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:02:46+00:00 2026-05-30T10:02:46+00:00

I implemented an exception class WINERR_EXCEPTION for throwing runtime description of GetLastError() ! And

  • 0

I implemented an exception class WINERR_EXCEPTION for throwing runtime description of GetLastError() !

And it worked as expected.
Then I wanted to eliminate the unnecessary allocations.
For that I implemented an interim class that takes and stores a const char* which is returned by what().

However when I use my interim_exception class the MSVC (c++11 developer preview) implementation of std::exception uses the std::exception baseclass member-variables (instead of what()) to copy the description-string.
and I get "unknown exception" as the return value of what().

standard says :

exception& operator=(const exception& rhs) noexcept;
    Effects: Copies an exception object.
    Postcondition: If *this and rhs both have dynamic type exception then strcmp(what(), rhs.what()) shall equal 0.

Has this something to do with the word dynamic in the Postcondition (my interim_exception’s description is not dynamically allocated) or is this just a wrong implementation ?

msvc’s std::exception::operator= () :

_EXCEPTION_INLINE exception& __CLR_OR_THIS_CALL exception::operator=(const exception& _That)
    {
    if (this != &_That)
        {
        _Tidy(); // resets members

        if (_That._Mydofree) // NB. This prevents my intentions (evals to false)
            {
            _Copy_str(_That._Mywhat);
            }
        else
            {
            _Mywhat = _That._Mywhat;
            }
        }

    return *this;
    }

my code :

////////////////////////////////////////////////////////////////////////////////
// file : TstThrow.h
// purpose : implementation of class WINERR_EXCEPTION
////////////////////////////////////////////////////////////////////////////////
//

#include <windows.h> 
#include <exception>
#include <stdexcept>

////////////////////////////////////////////////////////////////////////////////

namespace 
{
    // class interim_exception to avoid unnecessary allocations
    class interim_exception 
        : public std::exception
    {
    public: 
        // override member : what() const
    const char * what() const
    //_EXCEPTION_INLINE virtual const char * __CLR_OR_THIS_CALL what() const
    { return m_What; }

        interim_exception( const char* szWhat )
            :m_What( szWhat ? szWhat : "" ) {}

    private:
        const char* m_What;

    };
}

////////////////////////////////////////////////////////////////////////////////

class WINERR_EXCEPTION 
    : public std::exception
{
public:
    DWORD    ErrNo () const  { return m_ErrNo; }

public:
    explicit WINERR_EXCEPTION ( DWORD dwErrNo );

    inline exception& operator= ( const exception& that )
    {
        exception::operator= ( that );
        return (*this);
    }

    inline exception& operator= ( const char* szWhat )
    { 
        operator= ( std::exception( szWhat )); /* this works OK */
        // exception::operator= ( std::exception( szWhat )); /* this works OK */
        // exception::operator= (interim_exception( szWhat )); /* this doesn't work */
        // operator= (interim_exception( szWhat )); /* this doesn't work */
        return (*this);
    }

private:
    DWORD    m_ErrNo;

};

////////////////////////////////////////////////////////////////////////////////


WINERR_EXCEPTION::WINERR_EXCEPTION ( DWORD dwErrNo )
    :m_ErrNo (dwErrNo) //exception("") ,
{
    DWORD    dwFrmtFlags (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER);
    dwFrmtFlags |= FORMAT_MESSAGE_IGNORE_INSERTS;
    dwFrmtFlags |= FORMAT_MESSAGE_MAX_WIDTH_MASK;    // no newlines

    LPSTR    pBuffer (nullptr);
    if (! ::FormatMessageA( dwFrmtFlags ,nullptr 
                           ,dwErrNo ,0 ,(LPSTR)&pBuffer ,0 ,nullptr ))
    {
        dwErrNo = GetLastError();
        if (dwErrNo == ERROR_MR_MID_NOT_FOUND)
            operator= ( WINERR_EXCEPTION( dwErrNo ).what() );
        else
            operator= ( "Substituted Error Message :\n\t" 
                        "Could not allocate buffer for ORIGINAL Error Message\n" );
    }
    else
    {
        operator= ( pBuffer );
        LocalFree( pBuffer );
    }
}

////////////////////////////////////////////////////////////////////////////////

my test :

void     TstThrow ()
{
    for ( DWORD dwErr = ERROR_SUCCESS; dwErr < 200; ++dwErr )
    {
        SetLastError( dwErr );
        try
      {
          throw ::WINERR_EXCEPTION( GetLastError() );
      }
      catch (const ::WINERR_EXCEPTION& werr)
      {
          ::WINERR_EXCEPTION err ( werr ); // test for copying of object !

          std::cout << std::setw(4) << werr.ErrNo() << " :"<< werr.what() << std::endl;
        }

      if ((dwErr % 100) == 0)
          Sleep(1500);
    }
}
  • 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-30T10:02:48+00:00Added an answer on May 30, 2026 at 10:02 am

    The two objects “both have dynamic type exception” if the dynamic type of both objects is exception, and not a subtype of exception. So the postcondition doesn’t apply in your case, since the dynamic type is WINERR_EXCEPTION, not exception.

    If you want what() to return something other than the default provided by std::exception(), then you’ll have to either override it yourself, or inherit from something that overrides it, such as the exception types defined in <stdexcept>. (At least that’s the case in Standard C++; I don’t know exactly how Microsoft’s extensions to std::exception are supposed to work, so I can’t comment on them).

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

Sidebar

Related Questions

I've worked in shops where I've implemented Exception Handling into the event log, and
I need a class to navigate across a collection, then I implemented Iterator interface.
Implemented the Sink Class - to receive event notifications from COM Server Event Interface
First some background: I already have a Result class that I've implemented to carry
Two interfaces with same method names and signatures. But implemented by a single class
I have a custom exception class which contains some additional fields. I want these
I have implemented a class string, similar to std::string one. I have a problem
I have a thread implemented inside a class using runnable like this: static Runnable
I have written my own Exception (MyException) and implemented Logging and showing Error Messages
Possible Duplicate: Using global exception handling with “setUncaughtExceptionHandler” and “Toast” I have implemented UncaughtExceptionHandler

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.