Own Exception class derived from runtime_error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • amphetaman@gmail.com

    #1

    Own Exception class derived from runtime_error

    If I derive my own Exception class from std::runtime_er ror, do I have
    to write the destructor even if its body is empty?

    #include <stdexcept>

    class Exception : public std::runtime_er ror
    {
    public:
    explicit Exception(const std::string &description ) :
    std::runtime_er ror(description ) { }
    virtual ~Exception() throw() { } // Do I need to write this?
    };
  • Ian Collins

    #2
    Re: Own Exception class derived from runtime_error

    amphetaman@gmai l.com wrote:
    If I derive my own Exception class from std::runtime_er ror, do I have
    to write the destructor even if its body is empty?
    >
    No.

    --
    Ian Collins.

    Comment

    • amphetaman@gmail.com

      #3
      Re: Own Exception class derived from runtime_error

      On Jul 6, 4:03 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      ampheta...@gmai l.com wrote:
      If I derive my own Exception class from std::runtime_er ror, do I have
      to write the destructor even if its body is empty?
      >
      No.
      >
      --
      Ian Collins.
      And if I derive other classes from that Exception class?

      Comment

      • James Kanze

        #4
        Re: Own Exception class derived from runtime_error

        On Jul 6, 10:03 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        ampheta...@gmai l.com wrote:
        If I derive my own Exception class from std::runtime_er ror,
        do I have to write the destructor even if its body is empty?
        No.
        Maybe.

        The destructor of std::runtime_er ror has an empty exception
        specification, which means that all destructors of classes
        derived from it must also have empty exception specifications.
        §15.5/13 describes how the exception specification of an
        implicitly generated function is generated; the important point
        here is that the exception specification will only be empty if
        all of the functions called by the generated function also have
        empty exception specifications.

        If the compiler generated destructor only calls the
        std::runtime_er ror destructor, there is no problem. If the
        class has additional members of class type, however, there is
        likely to be a problem, because a lot of classes (e.g.
        std::string, boost::shared_p tr...) even if they never throw. If
        your derived class uses any of these, you'll need to declare and
        defined an empty destructor, with an empty exception
        specification (and exceptions are one case where
        boost::shared_p tr makes a lot of sense, since it is guaranteed
        to be able to copy without throwing---unlike classes like
        std::string, which may do a deep copy).

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • amphetaman@gmail.com

          #5
          Re: Own Exception class derived from runtime_error

          Thanks, guys!

          Comment

          Working...