472,958 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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


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
3 3438
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Noah Roberts | last post by:
I am having some problems with deriving the std::exception class. My compiler (g++-2.95) works with it just fine, but it does in a lot of broken cases. I have a user/developer that can't compile...
4
by: Boogie El Aceitoso | last post by:
Hi, I have an exception class for winapi errors (returns a formated error mesage, usign ::GetLastError() et al, with what()). It looks like this: class WinapiException : public...
2
by: Stefan Pantos | last post by:
Dear all, Could someone explain to me the proper use of throw()? As it is used for std::exception. I cannot find anything which describes how it should be used and all the information about...
3
by: __PPS__ | last post by:
Hi, I've read in documentation to different libraries that their exception classes aren't subclasses from std::exception, and a separate catch statement is required for their exceptions. Always, I...
3
by: bobueland | last post by:
Sometimes the best way to understand something is to understand the mechanism behind it. Maybe that is true for exceptions. This is a model I have right now (which probably is wrong) 1. When a...
4
by: Divick | last post by:
Hi all, I want to subclass std::exception so as to designate the type of error that I want to throw, out of my classes, and for that I need to store the messages inside the exception classes. I...
11
by: Mike - EMAIL IGNORED | last post by:
On FC4 with g++ using STL and Posix threads and sockets I am catching an exception that is not a std::exception. Any idea what it might be? An outline of my catch macro is below. Thanks,...
3
by: Sendil kumar | last post by:
Hi All, Problem Stetement:I have a problem in getting stack trace when I ues std::exception. In my code, I allocate virtual memory for certain kind of processing and will throw the...
3
by: vainstah | last post by:
Hello Guys and Galls, To start off, I have reached the solution I was looking for, but I would like comments and feedback on the solution I have reached and tips/tricks on making it more elegant....
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.