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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:13:18+00:00 2026-05-29T04:13:18+00:00

Some time ago, I found a clever trick on stackoverflow that showed a way

  • 0

Some time ago, I found a clever trick on stackoverflow that showed a way to create a derived exception class from runtime_error that accepts a wstring as a constructor. The trick was to initialize the runtime_error base with a virtual method call. Here’s what that looks like:

UnknownEx(const std::wstring& s) throw() : m_msg(s), std::runtime_error( what() ) {};

And what() does the conversion (I’m okay with the lossy conversion from wstring to string):

virtual const char* what() const throw() {
    // Convert the wstring data member to a char*.
    const wchar_t* wstr = m_msg.c_str();
    char *str = new char[wcslen(wstr)];
    size_t *charsChanged = 0;
    wcstombs_s(charsChanged, str, sizeof(str), wstr, _TRUNCATE);

    return str; 
}

This compiles fine, but when I try to utilize it:

throw UnknownEx(L"Test Exception");

The application fails in the midst of what() when trying to find the length of the data member m_msg. When debugging into what(), m_msg hasn’t yet been set. It seems this function call in the initializer list trick isn’t working.

Is there a way to get this “trick” to work properly, or is my entire approach misguided?

My long term goal: I would simply like to create a derived exception class that is able to be caught generically with catch(exception& ex), but accepts a wstring in its constructor.

  • 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-29T04:13:18+00:00Added an answer on May 29, 2026 at 4:13 am

    The program crashes because std::runtime_error(what()) is executed before m_msg(s). Base classes are initialized before member variables.

    While your call to what() will in this case call your UnknownEx override, it’s best not to call virtual functions from constructors, because the semantics are confusing.

    If you are going to store your exception message in your own m_msg member variable, don’t derive from std::runtime_error; there’s no reason to store the string twice. Consider deriving directly from std::exception.

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

Sidebar

Related Questions

I found some time ago a CSS framework like Clever CSS but I don't
Some time ago I used the (blazing fast) primesieve in python that I found
Some months ago I found a project-management / time-estimation software that would ask you
Some time ago I found a reference online to a database system that stored
some time ago I found an article ( Roles: Composable Units of Object Behavior
Some time ago I put together a time based library that could be used
Some time ago, I came across a piece of code, that used some piece
Some time ago, reading this entry I found a nice image and a pointer
Some time ago, reading the App Engine Cookbook, I found this recipe based on
Canvas from html5 was introduced some time ago. Currently it's used (almost) only for

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.