Reorder catch
blocks to fix this behavior:
#include <iostream> class EA {}; class EB: public EA {}; void F() { throw EB(); // throw at EB(). } int main() { try { F(); } catch(EB&) // why not me? every time? { std::cout<<"EB Exception"; } catch(EA&) // caught here?? { std::cout<<"EA Exception"; } std::cout<<" Finished"<<std::endl; return 0; }
The compiler even warns you about this:
main.cpp:21:3: warning: exception of type 'EB' will be caught catch(EB&) // why not me? every time? ^~~~~ main.cpp:17:3: warning: by earlier handler for 'EA' catch(EA&) // caught here?? ^~~~~
source share