Nesting try blocks inside catch

Discussion in 'C++' started by iftekhar, Jan 24, 2006.

  1. iftekhar

    iftekhar Guest

    hi there ,

    consider the followinf code
    #include <iostream>
    #include <stdexcept>

    using namespace std;

    int main (void)
    int i = 5;
    throw i;
    catch (int j)
    double d = 4.5;
    throw d;
    cout << "got d" << endl;
    int k = 5;
    throw k;
    catch (...)
    float f = 5.5;
    throw f;
    catch(float g)
    cout << "got float g" << endl;
    catch (...)
    cout << "unknown error" << endl;
    cout << "done" << endl;
    compiled with gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-52)

    I expected the output of the program to be
    got d
    got float g

    but the output is just
    got d

    i tried to debug it and it seems that the ' throw f; ' causes the
    program to receive SIGABRT
    and try to do an assembly level debug revealed it fails in the

    __cxa_throw function and then calls the terminate

    in a similar situation where objects are thrown rather than intrinsic
    types it throws logic_error

    any one have any idea why?

    thanks for reading.
    iftekhar, Jan 24, 2006
    1. Advertisements

  2. iftekhar

    Zara Guest

    // *** TRY _BLOCK A, any exception will be catched by CATCH_BLOCK_A_#
    // *** CATCH_BLOCK_A_1 will catch only from TRY_BLOCK_A
    // *** TRY _BLOCK B, any exception will be catched by CATCH_BLOCK_B
    // *** CATCH_BLOCK_B will catch only from TRY_BLOCK_B
    // *** TRY _BLOCK C, any exception will be catched by CATCH_BLOCK_C
    // *** CATCH_BLOCK_C will catch only from TRY_BLOCK_C
    // *** CATCH_BLOCK_A_2 will catch only from TRY_BLOCK_A
    // *** CATCH_BLOCK_A_3 will catch only from TRY_BLOCK_A
    As I hope that comments inserted will make clear, catches are assigned
    to tries. Once yopu are inside a catch, you are *outside* of the
    corresponding try. So your exception "i" exits TRY_BLOCK_A and enters
    CATCH_BLOCK_A_1. Exception "d" is thrown, thus exiting TRY_BLOCK_B and
    entering CATCH_BLOCK_B ("got d"). Exception "k" is thrown, exiting
    TRY_BLOCK_ C and entering CATCH_BLOCK_C. Exception "f" is thrown, and
    as there is no try block surrounding it, it is an unhandled exception.

    Rememeber: To be catched, an exception must be *surrounded* by a try
    block. When one is in a catch block, then it is outside of its
    corresponding try block.

    This is atndard, it si not compiler dependent.

    Best regards,

    Zara, Jan 25, 2006
    1. Advertisements

  3. iftekhar

    iftekhar Guest

    i got it this morning too. But my problem was not exactly this. The
    real code was throwing logic_error because i was passing NULL ponter to
    basic_string<>. And since i was debuging with eclipse i could not go
    very deep.
    iftekhar, Jan 25, 2006
    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.