How does C++ exception handling deal with exception derived classes?
c++, exception
Solution
It's easy to explain with code: http://ideone.com/5HLtZ
#include <iostream>
class ExceptionBase {
};
class MyException : public ExceptionBase {
};
int main()
{
try
{
throw MyException();
}
catch (MyException const& e) {
std::cout<<"catch 1"<<std::endl;
}
catch (ExceptionBase const& e) {
std::cout<<"should not catch 1"<<std::endl;
}
////////
try
{
throw MyException();
}
catch (ExceptionBase const& e) {
std::cout<<"catch 2"<<std::endl;
}
catch (...) {
std::cout<<"should not catch 2"<<std::endl;
}
return 0;
}
output: catch 1 catch 2
Problem
If I am catching `BaseException` will this also catch exceptions which derive from `BaseException`? Does exception handling care about inheritance, etc, or does it only match the exact exception type being caught? ``` class MyException { ... }; class MySpecialException : public MyException { ... }; ``` ``` void test() { try { ... } catch (MyException &e) { //will this catch MySpecialException? } } ```