detecting if no exception was thrown

A

Aryeh M. Friedman

I am writting a uit tsting framework and need to know is there any way to
detect if some expression throws a exception or not (I know how to handle
the case of it throwing but not the expected one). Here is my test macro so
far:

#define TEST_ASSERT_THROW(expr,x) \
try { expr; } catch(x) {test_pass(0);} catch(...) {test_fail(0);};

What I want is a test_fail(0) if no exception was thrown by expr.

--Aryeh
 
C

Clark S. Cox III

I am writting a uit tsting framework and need to know is there any way
to detect if some expression throws a exception or not (I know how to
handle the case of it throwing but not the expected one). Here is my
test macro so far:

#define TEST_ASSERT_THROW(expr,x) \
try { expr; } catch(x) {test_pass(0);} catch(...) {test_fail(0);};

What I want is a test_fail(0) if no exception was thrown by expr.

How about:

#define TEST_ASSERT_THROW(expr,x) \
{ \
bool caught_exception = false; \
try { expr; } catch(x) {caught_exception = true;} \
if(caught_exception) \
test_pass(0); \
else \
test_fail(0); \
};
 
K

Kurt Stutsman

Clark said:
How about:

#define TEST_ASSERT_THROW(expr,x) \
{ \
bool caught_exception = false; \
try { expr; } catch(x) {caught_exception = true;} \
if(caught_exception) \
test_pass(0); \
else \
test_fail(0); \
};

Or if you know that test_pass(0) doesn't throw x:

try {
expr;
test_pass(0);
} catch(x) {
test_fail(0);
}
 
A

Aryeh M. Friedman

Kurt Stutsman said:
Or if you know that test_pass(0) doesn't throw x:

try {
expr;
test_pass(0);
} catch(x) {
test_fail(0);
}

Actually the test only passes if it throws x

--Aryeh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top