Note that there are some explanatory texts on larger screens.

plurals
  1. POderiving from std::exception
    primarykey
    data
    text
    <p>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.</p> <p>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?</p> <pre><code>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; }; </code></pre> <p>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.</p> <p>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:</p> <pre><code>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; }; </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload