-4

as I am new in programming, I just wanted to know in the following code why we wrote const char* what() instead of char what below in 6th line. second, why someone wrote MyException& e not just MyException e why we used reference over here in the Catch block. please enlighten me as I am a little confused over references and const references something.......

#include <iostream>
#include <exception>
using namespace std;
class MyException: public exception{
    public:
const char* what(){
return "C++ exception";}
};
int main()
{
    try{
    throw MyException();}
    catch(MyException e){
    cout<<"Myexception caught"<<endl;
    cout<<e.what()<<endl;
    }
}

  • Pass by const reference to avoid unnecessary copies. Pass by reference if you want the option to modify the object. You don't want to work with a copy of the exception but with the thrown exception. A char pointer is usually a C string. A const char pointer can point to a string literal. – Thomas Sablik Jun 24 '20 at 21:29
  • 1
    Please only ask one question per post. Do you know what the types `char` and `const char*` each represent? – Brian61354270 Jun 24 '20 at 21:30
  • 1
    A `char` is a single character. Not so useful when you want to send more than one letter. `"C++ exception"` is a [String Literal](https://en.cppreference.com/w/cpp/language/string_literal), and a String literal is a constant array (`const char [exactly_the_right_size]`) of characters of exactly the right size to hold the string and its terminating null. This character array will [decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) to a pointer to a constant array of characters (a `const char *`) – user4581301 Jun 24 '20 at 21:32
  • 3
    This should all be covered early on in [a non-fraudulent C++ programming text](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Stack Overflow is not a replacement for good reference materials and the internet as a whole is more likely to make you a bad programmer than a good one. Get a book. Read the book. Do the practice exercises. Learn to program in a controlled environment and you'll shave years off of your learning experience. – user4581301 Jun 24 '20 at 21:35
  • 2
    @ThomasSablik catching by reference is not only about copying/not copying, but about not slicing, though doesnt matter here – 463035818_is_not_an_ai Jun 24 '20 at 21:38
  • 1
    @idclev463035818 Yes, you are right. I completely forgot about it. – Thomas Sablik Jun 24 '20 at 21:41

1 Answers1

2

There are two reasons to do so here. A char represents a single character, like a or ,. A const char* can represent an entire string of characters, so you could have:

char getChar() { return 'M'; }
const char* getWord() { return "String"; }

The second reason is because the class MyException is deriving from std::exception, which has an override-able (virtual) function called what, so when we throw MyException and catch it, when we call what() it returns "C++ exception"

MyException& is used because we don't need a copy, we only need a reference. Taking a copy could also lead to object slicing, which depending what you did with the caught exception could be bad. Also a copy would invoke the constructor which could throw, which would be a mess.

Tas
  • 7,023
  • 3
  • 36
  • 51
  • 1
    the more important reason to catch by reference is to avoid slicing (cf https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#e15-catch-exceptions-from-a-hierarchy-by-reference). OPs code could also catch a `std::exception&`, but catching a `std::exception` would be problematic if `MyException` had members – 463035818_is_not_an_ai Jun 24 '20 at 21:51