473,416 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 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 3470
"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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.