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. Advertising

  2. Pierre Rouleau

    Siemel Naran Guest

    "Pierre Rouleau" <> 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.
     
    Siemel Naran, Mar 3, 2005
    #2
    1. Advertising

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

    Siemel Naran wrote:

    > "Pierre Rouleau" <> 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.


    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

    "Pierre Rouleau" <> wrote in message
    news:LvZVd.53494
    > Siemel Naran wrote:


    > > All of the 5 functions of class std::exception are declared with

    throw(),
    > > according to the standard.

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


    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. 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. Kerri
    Replies:
    2
    Views:
    13,120
    Kevin Spencer
    Oct 27, 2003
  2. exquisitus
    Replies:
    2
    Views:
    7,564
    exquisitus
    May 11, 2005
  3. Replies:
    15
    Views:
    7,764
    Roedy Green
    Sep 8, 2005
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,250
    Smokey Grindel
    Dec 2, 2006
  5. Debajit Adhikary
    Replies:
    36
    Views:
    2,408
    Andre Kaufmann
    Feb 10, 2011
Loading...

Share This Page