3

I am trying to implement a new exception for our code base and I came across this post in SO:

https://stackoverflow.com/a/8152888/654019

but I am not sure why the what method is defined as follow:

virtual const char* what() const throw (){
    return msg_.c_str();
}

What is the meaning of throw in this context?

Community
  • 1
  • 1
mans
  • 17,104
  • 45
  • 172
  • 321

1 Answers1

1

This is dynamic exception specification, which is deprecated since C++11, and removed from C++17.

throw() is used to specify that the function won't throw any exceptions (directly or indirectly), from C++11 we should use noexcept or noexcept(true) instead.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • `noexcept` was added in C++11. So while it's correct to say that you shouldn't use it in C++17, you shouldn't use dynamic exception specifications with C++11 either. – Nicol Bolas Apr 02 '17 at 14:18
  • @NicolBolas You meant `throw()` shouldn't be used since C++11, all dynamic exception specifications shouldn't be used since C++17. Is the understanding correct? – songyuanyao Apr 02 '17 at 14:25
  • Well, you shouldn't be using any dynamic exception specifications in C++11, since they were deprecated. It's just that C++17 finally removed all of the non-empty ones. – Nicol Bolas Apr 02 '17 at 14:27
  • I believe VS 2013 doesn't implement them yet, so I stuck to this for aa while I think. – mans Apr 02 '17 at 14:30
  • @NicolBolas Yes I see. – songyuanyao Apr 02 '17 at 14:31