C++ Custom exception throw () in constructor -
so read aren't supposed have other basic types in custom exception class because otherwise might throw exception within exception (like dream within dream). , should throw value , catch ref.
i have exception class header:
class deepthroatexception : public std::runtime_error { public: deepthroatexception(const char* err); // set err member err private: // error description const char* err; };
but dont since introduces issue memory management, invisible naked eye , need use mine detector. change std::string
if possible.
but there issue in first paragraph above, thought doing this:
#include <string> class deepthroatexception : public std::runtime_error { public: deepthroatexception(std::string err) throw(); // set err member err, no throw private: // error description std::string err; };
is ok do?
using std::string
can give bad time std::bad_alloc
. problem inherent std::runtime_error
, constructors can take std::string
argument:
explicit runtime_error( const std::string& what_arg ); explicit runtime_error( const char* what_arg );
it's way because copying exceptions shall never throw, implementation allocate string , copy argument's content it. if don't want second exception being thrown, mark constructor noexcept
, make sure never fails, , if ever do, program shut down immediately.
you can inherit std::runtime_error
behavior constructing string constructor so:
deepthroatexception(const std::string& err) noexcept : std::runtime_error(err) { // ... }
at point, can remove err
data member, runtime_error
give internal string can refer what()
.
Comments
Post a Comment