0

I am starting out with c++, out of curiosity I am trying to catch exceptions with polymorphism. I am observing diff behaviours when trying to catch an std exception like bad_alloc vs my custom exception

In case 1 I am throwing a bad_alloc exception which is caught by the catch statement with the argument (exception e) the what statement displays the exception to be bad_alloc().

In case 2 I am throwing MyException which is derived from exception class this is caught by the catch statement with the argument (exception e) the what statement displays the exception to be an "Unknown exception" by which I could figure it was calling the what() function of the base class exception.

In case 3 I am able to call the what() function of child class MyException from the catch while using pointers and "My custom Exception" is getting printed

#include<iostream>
using namespace std;

class MyException : public exception {
public:

    const char* what() const throw() {
        return "My custom Exception";
    }
};
int main() {    

    try {
        bool case1 = false, case2=true, case3=true;
        if (case1) {
            throw bad_alloc(); 
        }
        if (case2) {
            throw MyException();
        }
        if (case3) {
            MyException* o = new MyException();
            throw o;
        }
        
    }   
    catch (exception* e) {
        cout << endl << "catching exception with polymorphism, exception type  is " << e->what();

    }
    catch (exception e) {
        cout << endl << "catching exception with polymorphism  exception type  is " << e.what();

    }

}

My doubt is how is the bad_alloc exception able to call the what() function of itself when there is a rule that Virtual functions(what() in this case) should be accessed using a pointer or reference of base class type to achieve run time polymorphism. Here in this scenario, the virtual function is being accessed by the child class object directly in polymorphism.

user438383
  • 5,716
  • 8
  • 28
  • 43
ArunJose
  • 1,999
  • 1
  • 10
  • 33
  • 1
    Try `catch (exception& e)`. – molbdnilo Aug 20 '21 at 07:43
  • 2
    You are slicing look here [slicing](https://stackoverflow.com/questions/274626/what-is-object-slicing) – Koronis Neilos Aug 20 '21 at 07:48
  • You have object slicing as expected for case 1 and 2. – Jarod42 Aug 20 '21 at 07:49
  • 1
    Example can be changed to test all cases with loop, for example like [that](https://coliru.stacked-crooked.com/a/33fb8f248af80a7c). – Jarod42 Aug 20 '21 at 07:50
  • @jarod42 with object slicing in case1 and case2 why am I able to call what() method of child in case 1 but in case 2 what() of parent is called. Can somebody explain why is this? – ArunJose Aug 20 '21 at 08:02
  • @molbdnilo by using reference I might get this working, as in the case of (exception* e) but my question is why bad_alloc exception works even without catch (exception& e) ? – ArunJose Aug 20 '21 at 08:07
  • In provided link, I got `std::exception` for both slicing cases, no `bad_alloc`... – Jarod42 Aug 20 '21 at 08:07
  • @jarod42 are you refering to my code? Not sure which link you meant in your comment when you said "provided link" – ArunJose Aug 20 '21 at 08:09
  • in my code you can make case1=true to get bad_alloc from e.what() – ArunJose Aug 20 '21 at 08:12
  • I rewrote your code to show all case in my second comment. and even if I use your code (by changing case1 to true), I got `std::exception` [Demo](https://coliru.stacked-crooked.com/a/c63772b9df7c9560). provide compiler might help to reproduce issue. – Jarod42 Aug 20 '21 at 08:20
  • @molbdnilo If you want to be fully correct then use catch(const exception& e) ;) – Pepijn Kramer Aug 20 '21 at 08:21
  • @Jarod42 I am using visual studio compiler on windows. Let me try this in g++ and look into exception and get back – ArunJose Aug 20 '21 at 08:27
  • @Jarod42 I am getting expected behaviour with g++, the issue is only with visual studio compiler. – ArunJose Aug 20 '21 at 08:43
  • I am getting issue is only with visual studio compiler on windows? I didnt know compilers and OS would cause such changes in c++, New learnings. – ArunJose Aug 20 '21 at 09:05
  • The standard doesn't define what `exception::what()` returns. An implementation may choose to make it so an `exception` copy-constructed from `bad_alloc` returns the string `"bad_alloc"` from `what()`; or it may choose not to. – Igor Tandetnik Aug 22 '21 at 05:12

0 Answers0