try--catch inside do...while

Discussion in 'C++' started by AB, Jun 2, 2006.

  1. AB

    AB Guest

    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.
     
    AB, Jun 2, 2006
    #1
    1. Advertisements

  2. What's the "condition" if the exception is thrown?
    It doesn't help. If you need compiler-specific information, post to
    the VC++ newsgroup: 'microsoft.public.vc.language'.

    V
     
    Victor Bazarov, Jun 2, 2006
    #2
    1. Advertisements

  3. AB

    Marcus Kwok Guest

    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, Jun 2, 2006
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.