Search code examples
c++exceptiontestinggoogletest

Getting exception message from ASSERT_THROW


I'm using googletest and in many of my tests I use the ASSERT_THROW command. The problem is that if, for example, it throws a different exception than the one I expected, all I get is:

Actual: it throws a different type.

Is there some way to get it to spit out the return value of what() or something?


Solution

  • You probably throw by pointer (using new keyword)

    throw new MyDerivedException();
    

    and expected to receive a non-pointer type of exception:

    EXPECT_THROW(blah, MyDerivedException);
    

    You should throw by value.

    throw MyDerivedException(); // notice lack of 'new'