exception handling

I found an example from an article about exception handling, but there wasn't explained whats the meaning of 'const throw()' beyond the what() method header is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// using standard exceptions
#include <iostream>
#include <exception>
using namespace std;

class myexception: public exception
{
   // What means 'const throw()'?
  virtual const char* what() const throw()
  {
    return "My exception happened";
  }
} myex;

int main () {
  try
  {
    throw myex;
  }
  catch (exception& e)
  {
    cout << e.what() << '\n';
  }
  return 0;
}
Last edited on
Armen Tsirunyan wrote:


Regarding the const throw() part:

const means that this function (which is a member function) will not change the observable state of the object it is called on. The compiler enforces this by not allowing you to call non-const methods from this one, and by not allowing you to modify the values of members.
throw() means that you promise to the compiler that this function will never allow an exception to be emitted. This is called an exception specification, and (long story short) is useless and possibly misleading.


https://stackoverflow.com/questions/5230463/what-does-this-function-declaration-mean-in-c
Thanks, now it's clear to me :)
Note that this way of using the throw() syntax on functions has been deprecated since C++11 and will be removed in C++20. The recommended way to specify that a function cannot throw exceptions nowadays is to use the noexcept specifier.
Topic archived. No new replies allowed.