Own Exception class derived from runtime_error

Discussion in 'C++' started by amphetaman@gmail.com, Jul 6, 2008.

  1. Guest

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

    #include <stdexcept>

    class Exception : public std::runtime_error
    {
    public:
    explicit Exception(const std::string &description) :
    std::runtime_error(description) { }
    virtual ~Exception() throw() { } // Do I need to write this?
    };
    , Jul 6, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

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

    No.

    --
    Ian Collins.
    Ian Collins, Jul 6, 2008
    #2
    1. Advertising

  3. Guest

    On Jul 6, 4:03 pm, Ian Collins <> wrote:
    > wrote:
    > > If I derive my own Exception class from std::runtime_error, 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?
    , Jul 6, 2008
    #3
  4. Guest

    On Jul 6, 3:02 pm, wrote:
    > On Jul 6, 4:03 pm, Ian Collins <> wrote:
    >
    > > wrote:
    > > > If I derive my own Exception class from std::runtime_error, 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?


    Every class takes care of itself. You don't need to worry about the
    base classes because the destructors are called recursively by code
    generated by the compiler. The order is the reverse of the
    construction order.

    Ali
    , Jul 7, 2008
    #4
  5. James Kanze Guest

    On Jul 6, 10:03 pm, Ian Collins <> wrote:
    > wrote:
    > > If I derive my own Exception class from std::runtime_error,
    > > do I have to write the destructor even if its body is empty?


    > No.


    Maybe.

    The destructor of std::runtime_error 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_error 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_ptr...) 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_ptr 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 7, 2008
    #5
  6. Guest

    Thanks, guys!
    , Jul 7, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    378
    myork
    May 23, 2007
  2. Replies:
    1
    Views:
    369
    Victor Bazarov
    May 23, 2007
  3. Replies:
    2
    Views:
    322
  4. David
    Replies:
    3
    Views:
    382
    Grizlyk
    Jan 29, 2008
  5. Nick Keighley

    deriving from std::runtime_error

    Nick Keighley, Aug 6, 2008, in forum: C++
    Replies:
    2
    Views:
    573
Loading...

Share This Page