throw() after member function name - what for?

K

k.w.

I saw something like this:

class Fred {
public:
void g() throw();
};

What is this throw() for? I did not even know that you could put it there.
Could anyone explain this to a C++ novice?
 
I

Ian Collins

k.w. said:
I saw something like this:

class Fred {
public:
void g() throw();
};

What is this throw() for? I did not even know that you could put it there.
Could anyone explain this to a C++ novice?
It states that the function will not throw any exceptions. The compiler
may be able to apply certain optimisations if a function only calls
others that do not throw exceptions.
 
A

Alf P. Steinbach

* k.w.:
I saw something like this:

class Fred {
public:
void g() throw();
};

What is this throw() for? I did not even know that you could put it there.
Could anyone explain this to a C++ novice?

It's an empty exception specification, saying that no exception will
ever propagate out of g().

If an attempt is made to propagate an exception out of g(), then with a
conforming compiler a special mechanism is invoked, which by default
ends up calling std::terminate.

However, you can't rely on your compiler supporting this mechanism, even
if it accepts exception specifications.
 

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
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top