0

In this code:

#include <iostream>
#include <exception>
using namespace std;

class myexception : public exception
{
     virtual const char* what() const throw()  //<---**** Stared statement.
    {
     return "My exception happened";
    }
};

What does the stared statement mean. What is the reason for using const keyword and the type char* ?

I am confused with both of the const keywords. I know the basic use of const like declaring a variable that will store a unchangeable value.....sort of like #define but when it is used with functions (such as in parameters) it is confusing. I am confused on the usage of char* instead of char in this line. Also what is the purpose of specifying const before throw()?

Natsu
  • 71
  • 1
  • 7
  • 1
    Maybe you can expand your question with what you found confusing with the explanation in your C++ reference? – jxh Jun 15 '16 at 08:03
  • 2
    First of all, *which* of the two `const` keywords do you mean? Secondly, you do know about function return values? – Some programmer dude Jun 15 '16 at 08:04
  • I am confused with both of the `const` keywords. I know the basic use of `const` like declaring a variable that will store a unchangeable value.....sort of like `#define` but when it is used with functions (such as in parameters) it is confusing. Yes, I know about function return values....I am confused on the usage of `char*` instead of `char` in this line. – Natsu Jun 15 '16 at 08:14
  • 1
    I think you need to [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start reading. It will tell you all you need about what the asterisk means in different places (hint: look for a chapter on *pointers*). – Some programmer dude Jun 15 '16 at 08:18
  • I know the * means your are creating a pointer `int *a = &b;` and also that it is also a dereferencing operator. So, what my doubt is whether the return value in the above code is of type `char` or `char*`. – Natsu Jun 15 '16 at 08:22

2 Answers2

2

[Update] Since the author is asking more questions, I'll update the answer with both parts.

First of all, this function is an override function of std::exception::what(), the base function is defined that way, so the overrided version has to be the same syntax.

Then let's split the statement into two parts.

  1. The return value const char*
  2. The part after what() const throw()

First, check the return statement return "My exception happened";, it's returning a const c-like string, the type is const char*, so the return value is defined that way.

Second, the const after what() indicates it's a const function that will not modify any member (except for mutable though) of the class. The throw() means it never throws.

Mine
  • 4,123
  • 1
  • 25
  • 46
  • Why does it have to be `char*` and not `char`? What difference will be caused? Also, what is the reason of specifying `throw()`....is it so that the function cannot throw an exception? Furthermore, what is the reason for specifying `const` in `const throw()` ? – Natsu Jun 15 '16 at 08:28
  • Probably you should ask these questions in the question, since it's only asking the `const char*` part in the original question. – Mine Jun 15 '16 at 08:35
0

The const char* mean that the caller to what() will receive a string and will not be able to change the string (the char that the pointer is pointing to). This is an overriding of std::exception::what() so you can return a new explanation to your own exception (this is why you must return const char*, the original method does too)

gbehar
  • 1,229
  • 10
  • 10
  • So if the type was only `char`, I would not be able to return a whole string of characters? – Natsu Jun 15 '16 at 08:33
  • a char would mean passing 1 char by value. char* tells that somewhere there is a consecutive set of chars in the memory representing a string, and the char* is pointing to it. This is known as a c-style string (because that's how to pass strings in c) – gbehar Jun 15 '16 at 08:38