473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

detecting if no exception was thrown

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
4 1496
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
3
by: Karthik | last post by:
Hi, I am writing this application that needs a lot of arithmetic calculations. I was wondering if C++ language specifies any way of detecting arithmetic overflows. Let us consider the following...
5
by: n_o_s_p_a__m | last post by:
Can't compile. Does this mean that all functions that throw exceptions must be of return type void? examples: // won't compile: "not all code paths return a value" public override int Run() {...
7
by: Pi | last post by:
I have an instance of a TcpClient using a NetworkStream and I want to be able to detect when the connection is lost (or cable unplugged). What is the best way of doing this? I don't want to use...
12
by: ABN | last post by:
I have a C# (.NET 1.1) application in which I loop over a number of files on the hard drive and delete them. A few times, I've experienced an exception that says the file is in use by another...
7
by: pocmatos | last post by:
Hi all, What the best way to detect under/over flow in a program with a lot of computations? For example: #include <iostream> #include <limits> using namespace std;
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
15
by: RobG | last post by:
When using createEvent, an eventType parameter must be provided as an argument. This can be one of those specified in DOM 2 or 3 Events, or it might be a proprietary eventType. My problem is...
2
by: hharry | last post by:
Hello All, Does anyone know of a method to automatically detect if a file is corrupted ? Due to a failed backup process a number of files were corrupted. The files are mostly .xls, .doc, .pdf....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.