By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

detecting if no exception was thrown

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2005-02-18 23:17:55 -0500, "Aryeh M. Friedman"
<ar***@m-net.arbornet.org> said:
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); \
};

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #2

P: n/a
Clark S. Cox III wrote:
On 2005-02-18 23:17:55 -0500, "Aryeh M. Friedman"
<ar***@m-net.arbornet.org> said:
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); \
};


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

try {
expr;
test_pass(0);
} catch(x) {
test_fail(0);
}
Jul 23 '05 #3

P: n/a

"Kurt Stutsman" <ks*******@NOSPAM.sbcglobal.net> wrote in message
news:SN*****************@newssvr14.news.prodigy.co m...
Clark S. Cox III wrote:
On 2005-02-18 23:17:55 -0500, "Aryeh M. Friedman"
<ar***@m-net.arbornet.org> said:
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); \
};


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
Jul 23 '05 #4

P: n/a
Aryeh M. Friedman wrote:
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


Then just exchange test_pass and test_fail.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.