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

Discussion in 'C++' started by Pierre Rouleau, Mar 2, 2005.

  1. 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.
    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?

    class MyException : public std::exception
    {
    public:
    explicit MyException(int someinfo) throw();
    // violates specs of std::exception
    .....
    };

    Thanks for any information on this topic.

    Pierre R.
     
    Pierre Rouleau, Mar 2, 2005
    #1
    1. Advertisements

  2. Pierre Rouleau

    Siemel Naran Guest

    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.

    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; }
    }
     
    Siemel Naran, Mar 3, 2005
    #2
    1. Advertisements

  3. Thanks for the good explanation on the topic. I should have mentionned
    that I was using an old (and non compliant) compiler (VC6) where the
    exception class member functions are defined without the throw()
    specification.

    We also have Visual Studio .Net 2003 (VC7) and the <exception> file that
    comes with that version of the compiler does not have the throw()
    specification either.

    I was under the impression that VC7 was C++ standard compliant. It
    appears not. Are there newer version of the header files available for
    Microsoft VC7?

    Thanks again.

    Pierre Rouleau
     
    Pierre Rouleau, Mar 4, 2005
    #3
  4. Pierre Rouleau

    Siemel Naran Guest

    Don't know about MSVC 7 and exceptions, and the best place to ask in the
    microsoft newsgroups (there's one in usenet, and may in msdn.microsoft.com).

    Anyway, if the base class constructor declares with the throw spec, the
    derived class constructor can declare without the throw spec. And vice
    versa: if the base class constructor declares without the throw spec, the
    derived class constructor can declare with the throw spec. So whether or
    not the MSVC <exception> headers are compliant, I think should not matter.
     
    Siemel Naran, Mar 4, 2005
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.