Re: Why does std::exception constructor specifies it can throw any ?

From: Siemel Naran (SiemelNaran_at_REMOVE.att.net)
Date: 03/03/05


Date: Thu, 03 Mar 2005 06:52:19 GMT


"Pierre Rouleau" <Pierre_Rouleau@impathnetworks.com> wrote in message
news:J6nVd.29482

> The std::exception class defined in the Standard C++ <exception> header
> specifies that the constructors could throw any exception becuase they
> do not have a throw() specification.
>
> Why is that? Is this because there could be an exception thrown when
> the code creates a std::exception? I would assume that is not the case.

All of the 5 functions of class std::exception are declared with throw(),
according to the standard. The constructor, copy constructor, operator=,
and destructor do nothing (though the standard does not require that they do
nothing), so of course they don't throw. The pure virtual what function
must be defined in the derived classes as not throwing exceptions either (so
it will have to return a string stored inside the exception object).

But the constructor of the derived class may throw. If your exception
stores the names and values of the environment variables, this means
(usually) allocating dynamic memory to store all the info, and thus the
constructor may throw if it runs out of memory.

The throw() specification on a function does not just mean that the function
does not throw, but rather that the function throws nothing or calls
std::terminate. The call to std::terminate happens if the function throws
an exception either directly or calls a function (virtual or not) that
throws an exception.

If you declare your derived class as

class DerivedException : public std::exception {
public:
   DerivedException() throw() { }
};

does it fail compile? It passes compile on my computer, Borland C++ 6.

> However, if I want to create a new exception class, derived from
> std::exception (say MyException) then how can I guarantee that creating
> an instance of MyException will not generate any exception?

You can use try-catch.

DerivedException::DerivedException() {
try { stuff that may throw; }
catch (...) { }
}

But be aware that the compiler generated initialization list calls the
default constructor of the base class, plus the default constructor of
contained objects. If any of these throw, then you program will call
std::terminate. So be sure that the base class constructor does not throw,
which is the case for std::exception, and that the constructors of your
contained objects don't throw.

To garauntee this, you could allocate objects on the heap, for example:

class MyException : public std::exception {
public:
   MyException() throw();
   const char * what() const throw() { return d_what; }
private:
   struct Details;
   Details * d_details;
   const char *const d_what;
};

struct MyException::Details {
   std::string name;
   std::string value;
}

MyException::MyException() : std::exception(), d_details(), d_what() {
   try {
      d_details = new Details;
      d_details->name = "hello";
      d_details->value = "world";
   }
   catch (...) { delete d_details; d_what = NULL; }
}

> class MyException : public std::exception
> {
> public:
> explicit MyException(int someinfo) throw();
> // violates specs of std::exception
> ....
> };
>
> Thanks for any information on this topic.



Relevant Pages

  • Re: DbC & Exceptions & Style
    ... exception. ... Different ways the constructor can fail ... to avoid writing code that converts GUI data into objects, ... All domain objects must now provide a default constructor. ...
    (comp.object)
  • Re: Controlled types and exception safety
    ... >> propagate an exception. ... >> For an Adjust invoked as part of an assignment operation, ... But a user-defined constructor is ... a user-defined constructor has just turned on the ...
    (comp.lang.ada)
  • Re: Question concerning object-oriented programming
    ... constructor the inspectof the state fails to verify key ... Better yet the constructor could throw an exception. ... opposed to stateless) object can know if it is in an invalid state. ...
    (comp.programming)
  • Re: is such exception handling approach good?
    ... There is nothing wrong with throwing from constructor. ... It may be a good design, it may not be a good design from user's point ... resource API to free-up the resource on exception. ... change something - that is not initialization. ...
    (microsoft.public.vc.language)
  • Re: Initialization lists and optimization
    ... general the compiler cannot do it this way. ... > the constructor body. ... Not exactly true on "if 'x' were of a user-defined type with a default ... constructor that calls the default constructor of the contained objects. ...
    (comp.lang.cpp)