Murali
Murali

Reputation: 1516

what does this declaration mean? exception() throw()

std::exception class is defined as follows

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

what does the throw() syntax mean in a declaration? Can throw() take parameters? What does no parameters mean?

Upvotes: 8

Views: 5272

Answers (5)

Leandro T. C. Melo
Leandro T. C. Melo

Reputation: 4032

Without any parameter, it means that the mentioned functions does not throw any exceptions.

If you specify anything as a parameter, you're saying that the function will throw only exceptions of that type. Notice, however, this is not an enforcement to the compiler. If an exception of some other type happens to be thrown the program will call std::terminate().

Upvotes: 12

martsbradley
martsbradley

Reputation: 158

Can throw() take parameters?

Yes it can be used to declare what parameteres the method is allowed to throw.

Also the destructor is marked as throw(), destructors should never ever throw exceptions as they may already be executing in the context of a thrown exception.

Upvotes: 0

RC.
RC.

Reputation: 28217

This is called a throw specification. It defines which exceptions (if any) can be thrown from the function.

These sound great in theory, but there are issues with using them.

A good discussion about this can be found at this SO question.

Upvotes: 1

anon
anon

Reputation:

It's an "exception specification". throw() means "this function will not throw any exceptions". You can also specify exceptions, so throw(foo) would say this function may throw exceptions of type foo.

The usefulness of this feature has been debated quite a bit in the C++ community - the general evaluation seems to be that it is not particularly useful. For more details take a look at this Herb Sutter article.

Upvotes: 18

James
James

Reputation: 25533

It's an exception specification. No arguments means that the function can't throw any exceptions.

Upvotes: 1

Related Questions