473,396 Members | 2,011 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,396 software developers and data experts.

Deriving subclasses of std::exception and throw()

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
2 2208
* 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
> 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 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...
3
by: Pierre Rouleau | last post by:
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? ...
2
by: ma740988 | last post by:
The hierachy for standard exception lists: bad_alloc, bad_cast, bad_typeid, logic_error, ios_base::failure, runtime_error and bad_exception runtime_error and logic_error has subsets. The...
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...
5
by: Miles | last post by:
Hello all, When I subclass std::exception and throw an instance of the subclass, when I call the what() method of the caught exception, it does not call the overridden method. I'm under 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.