Deriving subclasses of std::exception and throw()

S

Stefan Pantos

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 exceptions in c++ seem to ignore
it completly. I know that it's similar to throws in java but I have no
idea of how it should be used.

When I say throw() I mean in the way it is used here:
class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
/** Returns a C-style character string describing the general cause
* of the current error. */
virtual const char* what() const throw();
} };

Thanks,
Stefan
 
A

Alf P. Steinbach

* Stefan Pantos said:
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 exceptions in c++ seem to ignore
it completly. I know that it's similar to throws in java but I have no
idea of how it should be used.

When I say throw() I mean in the way it is used here:
class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
/** Returns a C-style character string describing the general cause
* of the current error. */
virtual const char* what() const throw();
} };


That's an exception specification.

Java is based on static checking of exception specifications, whereas C++ is
based on dynamic checking.

That means essentially that the only exception specifications that are of
value in C++ are those where you definitely want the program to abort when or
if the specification is violated. 'throw()' is useful, and 'throw(
std::exception )' _can_ be useful, but opinions differ. Anything else should
probably be avoided.
 
D

Derek

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 exceptions in c++ seem to ignore it
completly. I know that it's similar to throws in java but
I have no idea of how it should be used.

When I say throw() I mean in the way it is used here:

class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
virtual const char* what() const throw();
}

Thanks, Stefan

It's an exception specification. In this case "throw()"
means the what() function does not throw an exception.
General opinion seems to be that using any exception
specification other than throw() is a bad idea. Here is
a good discussion: http://www.gotw.ca/gotw/082.htm
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top