By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

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

P: n/a

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.
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Pierre Rouleau" <Pi************@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.


Jul 23 '05 #2

P: n/a
Siemel Naran wrote:
"Pierre Rouleau" <Pi************@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.


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
Jul 23 '05 #3

P: n/a
"Pierre Rouleau" <Pi************@impathnetworks.com> 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.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.