You should make the extend function virtual, and return a DerivedException& in the DerivedException's override. This is legal only for special cases where the return value can be implicitly casted - such as where the original function signature returns a base class of the override function signature's return value, as I show below.
class BaseException {
public:
BaseException() { }
virtual BaseException& extend( std::string message ) {
// extend message
return *this;
}
};
class DerivedException : public BaseException {
public:
virtual DerivedException& extend( std::string message ) {
return *this;
}
DerivedException(std::string) : BaseException() { }
};
// RUNNING CODE
int main() {
try {
try {
// Something goes wrong
throw DerivedException( "message1" );
}
catch ( DerivedException& exc ) {
throw exc.extend( "message2" );
}
}
catch ( DerivedException& ) {
std::cout << "DerivedException!";
}
catch ( BaseException& ) {
std::cout << "BaseException!";
}
std::cin.get();
}
Displays DerivedException!