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

Deriving subclasses of std::exception and throw()

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* Stefan Pantos <st***********@chem.ox.ac.uk> schriebt:

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

P: n/a
> 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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.