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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:30:26+00:00 2026-05-19T17:30:26+00:00

i want to derive from std::exception to add specific information to my log files,

  • 0

i want to derive from std::exception to add specific information to my log files, but i cant figure how to access the .what() from the std::exception.

furthermore, i know that it is unsafe to create a string in my exception handler, but i’m not an expert on this topic, so what are some safer alternatives?

struct Exception : public std::exception, private boost::noncopyable
{
    public:
        Exception(std::string msg)
            : message(msg)
        {}
        ~Exception()
        {}

        virtual const char* what() const throw 
        {
            std::string what = message + // and now what? base.what()
            LOG(what); // write to log file
            return what.c_str();
        }

    private:
        std::string message;
};

EDIT:
i really have asked my question the wrong way. i’m rather interested in safety, i just thought it’d be nice to have more data for logging. i was wrong.

now, i’m not being so paranoid about bad_alloc being thrown by the message string in case there was a bad_alloc before, i’d rather have a neat message. that being said i rewrote some stuff:

struct Exception : public std::exception
{
    public:
        Exception(std::string msg)
            : message(msg)
        {}
        ~Exception()
        {}

        virtual const char* what() const throw 
        {
            LOG(what); // write to log file
            return what.c_str();
        }

    private:
        std::string message;
};

are there still any big concerns about that code now? the LOG() throws std::exception i case something goes wrong, because i didn’t want an infinite loop of log calling by derived exception class, and that class again calling log which would cause the same exeception again.
Will this work like i want it to, or will a logging exception in my derived class call terminate() or cause core dump?

  • 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-19T17:30:27+00:00Added an answer on May 19, 2026 at 5:30 pm

    EDIT: Since writing this answer, I’ve stumbled upon the Error and Exception Handling section in the Boost document. I would recommend that document over this answer.


    First off, making your exception not-copyable is a bad idea. When you write something such as

    // could be any exception, doesn't matter.
    throw Exception(...);
    

    The runtime creates a copy of that object into a special location. Some compilers might optimize this and create the original object in that location, but the The C++ Programming Language says it’s a copy, and I also believe that’s what the standard says, though I’m not sure. You might get away with this in your current environment, but that might not always be the case.

    Then, everything else depends on how paranoid you are with corner cases.

    The memory allocation part is mostly flaky in the exception clause (i.e. the constructor). If this memory allocation happens to fail (i.e. std::bad_alloc is thrown), there are two possibilities, depending on how you write your throw statement:

    1. std::string is created before the throw statement, std::bad_alloc replaces the exception you thought you would raise, problem is sort-of badly reported.
    2. std::string is created inline in the constructor call. If this is considered “during exception handling” by the standard, std::unexpected()/std::terminate() will be invoked and you basically get a core dump.

    In any case, it seems like you won’t get the desired effect of reporting your error.

    I always recommend to create some sort of temporary state that doesn’t allocate memory in the constructor and wait for the call to std::what() to create the string that reports the error, but that might still lead to case #1. You could resort to some compile-time determined buffer size to make sure that doesn’t happen.

    Many people will tell you they’ve never had problems with allocating strings in constructors because it’s unlikely std::bad_alloc will be raised unless the original exception was std::bad_alloc in the first place. Hence, it depends on your level of paranoia.

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

Sidebar

Related Questions

I want to derive from System.Windows.Controls.TextBox and provide this functionality. IsEnabledProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata( new
Class A derive from class B. In class A I want to hide some
I want to refactor multiple classes that I think should derive from one base
I use a class A from a library and want to add some functionality
I have a window (derived from JFrame) and I want to disable the close
I have a my custom collection derived from List(.MyItem.) I want to trace if
I'm currently investigating the possibility to use custom attributes derived from ActionFilterAttribute. I want
i want to copy a directory from one drive to another drive. My selected
Basically, I want to have my program retrieve various variables from the hard drive
I want to derive a stringstream so that I can use the operator<< to

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.