C ++ Should I write a throw clause for a function everywhere?

Front

Consider a class and a global function:

This is for example usefulfuncts.hpp

 void dosome(int a, int b) throw (std::exception); 

This is usefulfuncts.cpp

 void dosome(int a, int b) throw (std::exception) { //... } 

And this is aclass.hpp

 class aclass { // Members... friend void dosome(int a, int b) throw (std::exception); // Members... }; 

After (what I would like)

Ok! I would like to understand whether you need to write strictly every time in the throw clause. So, for example, can I do this?

This is usefulfuncts.hpp

 void dosome(int a, int b) throw (std::exception); 

This is usefulfuncts.cpp

 void dosome(int a, int b) { /* OMITTING IT! */ //... } 

And this is aclass.hpp

 class aclass { // Members... friend void dosome(int a, int b); /* OMITTING IT */ // Members... }; 

Is it correct? Put this only in the main declaration? Thanks

+4
source share
3 answers

Omitting the specification of an exception means that your function can throw any .

Exception specifications are bad. There are hardly any compilers that correctly implement this function. They are deprecated from standard C ++ 11. In fact, the exception specifications were considered an unsuccessful experiment, even when they were part of the C ++ 03 standard.

Good reading:
A pragmatic look at exception specifications

+12
source

Declaring exceptions is a bad idea. From http://www.gotw.ca/publications/mill22.htm :

Moral # 1: Never write an exception specification.
Moral No. 2: except perhaps empty, but if I were you, even avoid this.

+3
source

Exception specifications are deprecated in C ++ 11 - do not write exception specifications unless you indicate that your function is guaranteed to not throw an exception.

Please note that actually providing this guarantee is more complicated than it might seem. Herb Sutter has written many articles and a book on exceptions and exception safety.

+1
source

All Articles