Starting to use the googletest ASSERT_THROW clause, it seems that ‘sometimes’ the base type of the thrown exception is handled. I reduced the relevant code section to this:
// myexception.h
struct myexception : public std::logic_error {
myexception(const char* what):std::logic_error(what){}
};
void throwMy();
// myexception.cpp
void throwMy(){ throw myexception(NULL); }
This is my test code:
//
void localThrowMy(){ throw myexception(""); }
// test code, based upon the ASSERT_THROW macro
try {
throwMy(); // outputs "logic_error"
//localThrowMy(); // would output "what I expected"
}
catch( myexception & ) { cout << "what I expected"; }
catch( std::logic_error & ) { cout << "logic_error"; } // my addition
catch(...) { cout << "mmmh."; }
The strange thing is: if I declare the throwMy function in the same compilation unit as the test code, “what I expected” is output. If it’s in another unit, the test outputs “logic_error.”.
- g++ –version: (Debian 4.4.5-8) 4.4.5
- ld –version: (GNU Binutils for Debian) 2.20.1-system.20100303
Is this code correct? Could the linker be in error here?
Mea Culpa.
Once again, select wasn’t broken.
The
logic_errorwas thrown within thestd::stringconstructor: “basic_string::_S_construct NULL not valid”. I falsely deduced that there was a problem with the inheritance; it was a plain case of pebkac.Actually, I didn’t know. But you can’t pass a null pointer into the
std::stringconstructor.