EXPECT_EXCEPTION

1,228 views
Skip to first unread message

smadden

unread,
Apr 8, 2009, 1:13:14 PM4/8/09
to Google C++ Testing Framework
I have a piece of code that throws an exception for Zero (0) division.
I would like to test it.

Just to see if I could get it to pass, I tried:

EXPECT_EXCEPTION( Div(0, 0), std::invalid_argument);

This failed. However, when I do the following:

EXPECT_ANY_EXCEPTION( Div(0, 0))
It passes. And, the exception thrown that was shown was
std::invalid_argument.

Anyone have any advice as to how to use EXPECT_EXCEPTION correctly to
check for a specific argument? Cause I must be doing something wrong.

Zhanyong Wan (λx.x x)

unread,
Apr 8, 2009, 1:48:20 PM4/8/09
to smadden, Google C++ Testing Framework
On Wed, Apr 8, 2009 at 10:13 AM, smadden <stephen...@cox.net> wrote:
>
>
> I have a piece of code that throws an exception for Zero (0) division.
> I would like to test it.
>
> Just to see if I could get it to pass, I tried:
>
> EXPECT_EXCEPTION( Div(0, 0), std::invalid_argument);

Just to clarify: the macro is named EXPECT_THROW.

> This failed. However, when I do the following:
>
> EXPECT_ANY_EXCEPTION( Div(0, 0))
>     It passes. And, the exception thrown that was shown was
> std::invalid_argument.
>
> Anyone have any advice as to how to use EXPECT_EXCEPTION correctly to
> check for a specific argument? Cause I must be doing something wrong.

Please provide more information. How's Div() defined? Which OS and
compiler are you using?

On Linux using gcc, the following definition of Div() causes your code
to be killed by a signal instead of an exception:

int Div(int m, int n) { return m/n; }

--
Zhanyong

smadden

unread,
Apr 8, 2009, 2:01:26 PM4/8/09
to Google C++ Testing Framework
Yeah, sorry, I meant "EXPECT_THROW". I am using Visual Studio.net 2005
on an XP Box. I have Div defined exactly as your example.

On Apr 8, 10:48 am, Zhanyong Wan (λx.x x) <w...@google.com> wrote:

smadden

unread,
Apr 8, 2009, 2:02:49 PM4/8/09
to Google C++ Testing Framework
I meant Div is defined much in the way you had.

On Apr 8, 10:48 am, Zhanyong Wan (λx.x x) <w...@google.com> wrote:

Vlad Losev

unread,
Apr 8, 2009, 3:06:37 PM4/8/09
to smadden, Google C++ Testing Framework
Hi Stephen,

Dividing by 0 with the / operator will generate a structured exception. Structured exceptions indicate unexpected events such as division by zero or access violation which are usually detected by hardware (see the list here). In theory structured exceptions are totally independent from C++ exceptions, but Microsoft instrumented its C++ compiler to have the "catch(...)" construct intercept structured exceptions. EXPECT_ANY_THROW() uses it, so currently it is the only way you can test for them.

You should not expect the statement "x/0" throw a C++ exception. You may, however, change your implementation of Div to check the divider and throw std::invalid_argument yourself if the divider is 0. Then EXPECT_THROW( Div(0, 0), std::invalid_argument) will work as expected.

Regards,
Vlad.

smadden

unread,
Apr 8, 2009, 3:23:21 PM4/8/09
to Google C++ Testing Framework
Here was the solution:

in my math.h file I had

....

throw new exception;

....

I took "new" out, and everything works just fine.

On Apr 8, 12:06 pm, Vlad Losev <vladlo...@gmail.com> wrote:
> Hi Stephen,
>
> Dividing by 0 with the / operator will generate a structured exception.
> Structured exceptions indicate unexpected events such as division by zero or
> access violation which are usually detected by hardware (see the list
> here<http://msdn.microsoft.com/en-us/library/ms679356%28VS.85%29.aspx>).
> In theory structured exceptions are totally independent from C++ exceptions,
> but Microsoft instrumented its C++ compiler to have the "catch(...)"
> construct intercept structured exceptions. EXPECT_ANY_THROW() uses it, so
> currently it is the only way you can test for them.
>
> You should not expect the statement "x/0" throw a C++ exception. You may,
> however, change your implementation of Div to check the divider and throw
> std::invalid_argument yourself if the divider is 0. Then EXPECT_THROW(
> Div(0, 0), std::invalid_argument) will work as expected.
>
> Regards,
> Vlad.
>
Reply all
Reply to author
Forward
0 new messages