Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Own Exception class derived from runtime_error

Reply
Thread Tools

Own Exception class derived from runtime_error

 
 
amphetaman@gmail.com
Guest
Posts: n/a
 
      07-06-2008
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?
};
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-06-2008
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.
 
Reply With Quote
 
 
 
 
amphetaman@gmail.com
Guest
Posts: n/a
 
      07-06-2008
On Jul 6, 4:03 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> ampheta...@gmail.com 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?
 
Reply With Quote
 
acehreli@gmail.com
Guest
Posts: n/a
 
      07-07-2008
On Jul 6, 3:02*pm, ampheta...@gmail.com wrote:
> On Jul 6, 4:03 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
> > ampheta...@gmail.com 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-07-2008
On Jul 6, 10:03 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> ampheta...@gmail.com 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
 
Reply With Quote
 
amphetaman@gmail.com
Guest
Posts: n/a
 
      07-07-2008
Thanks, guys!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deriving from std::runtime_error Nick Keighley C++ 2 08-06-2008 12:37 PM
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Is an exception specification needed if the method throws and catchesits own exception? elcapitan666@gmail.com C++ 2 11-27-2007 04:01 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments