C++: Throwing a derived class by reference does not work when catching base class
I want to throw my own exceptions with the base class Exception . There is a virtual method print which will be overwritten by the subclasses. I only catch the type Exception& and use print to get the specific error. The problem is that once I throw a reference of a subclass it is trated as if it were the base class. Here is an example: #include <iostream> using namespace std; class Exception { public: virtual void print() { cout << "Exception" << endl; } }; class IllegalArgumentException : public Exception { public: virtual void print() { cout << "IllegalArgumentException" << endl; } }; int