Question about Tutorial

I have a question about some code from the "Exceptions" tutorial on this site under the "standard exceptions" section. Here's a link:

http://cplusplus.com/doc/tutorial/exceptions/

And the code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// standard exceptions
#include <iostream>
#include <exception>
using namespace std;

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

int main () {
  try
  {
    throw myex;
  }
  catch (exception& e)
  {
    cout << e.what() << endl;
  }
  return 0;
}


Specifically, I am confused about line 8. I know that putting throw() after a function definition means it cannot throw any exceptions. But what does const throw() do? Does this mean the function cannot throw exceptions with constant types?

Also, why is the virtual keyword included in the definition of the derived class? Wouldn't it already be included in the definition of the exceptions class, the base class?

Thanks.
const is unrelated to throw(). const means that the method will not modify any members of the class. virtual could be omitted, but is kept for clarity.
Okay, thanks. Makes sense now.
Topic archived. No new replies allowed.