try--catch inside do...while

A

AB

Hi All,

I've got a section of code which resembles

do
{
try...........(1)
{
//do something here
}
catch(exception)
{
//handle exception
}
}
while(condition) ;

//resume execution............(2)

If the exception is not thrown, the loop continues normally. However,
when an exception is caught...the catch block is executed after which
control moves to (2) and not back to (1) as I expected.

Can anyone tell me what I'm missing out here? If it helps...I'm using
the MSVC 8.0 compiler.
 
V

Victor Bazarov

AB said:
I've got a section of code which resembles

do
{
try...........(1)
{
//do something here
}
catch(exception)
{
//handle exception
}
}
while(condition) ;

//resume execution............(2)

If the exception is not thrown, the loop continues normally. However,
when an exception is caught...the catch block is executed after which
control moves to (2) and not back to (1) as I expected.

What's the "condition" if the exception is thrown?
Can anyone tell me what I'm missing out here? If it helps...I'm using
the MSVC 8.0 compiler.

It doesn't help. If you need compiler-specific information, post to
the VC++ newsgroup: 'microsoft.public.vc.language'.

V
 
M

Marcus Kwok

AB said:
I've got a section of code which resembles

do
{
try...........(1)
{
//do something here
}
catch(exception)
{
//handle exception
}
}
while(condition) ;

//resume execution............(2)

If the exception is not thrown, the loop continues normally. However,
when an exception is caught...the catch block is executed after which
control moves to (2) and not back to (1) as I expected.

What happens when you run this program?


#include <iostream>

int main()
{
int i = 0;

do {
try {
std::cout << "i = " << i << '\n';
++i;
if (i == 2) {
throw 2;
}
}
catch (int e) {
std::cout << "caught " << e << '\n';
}
} while (i < 4);
}


Using MSVC 7.1 my output is:

i = 0
i = 1
caught 2
i = 2
i = 3
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top