Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASSERT_THROW thinks my exception is of a different type

Tags:

c++

googletest

In Google Test, when I run the following test:

void ThrowInvalidArgument()
{
   throw new std::invalid_argument("I am thrown an invalid_argument");
}

TEST(ExpectExceptions, Negative)
{
  ASSERT_THROW(ThrowInvalidArgument(), std::invalid_argument);
}

I get the following failure:

error: Expected: ThrowInvalidArgument() throws an exception
                 of type std::invalid_argument.
       Actual: it throws a different type.
[  FAILED  ] ExpectExceptions.Negative (1 ms)

What am I doing wrong?

like image 549
PortMan Avatar asked Oct 22 '25 05:10

PortMan


1 Answers

You are throwing an instance of the std::invalid_argument* type, that is, a pointer.

Throw an object instead:

void ThrowInvalidArgument()
{
     throw std::invalid_argument("I am thrown an invalid_argument");
     //   ^ (no new)
}
like image 79
Piotr Skotnicki Avatar answered Oct 23 '25 19:10

Piotr Skotnicki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!