Exception handing problem??

Discussion in 'C++' started by pacificcoral, Mar 24, 2006.

  1. pacificcoral

    pacificcoral New Member

    Joined:
    Mar 24, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I am a C programmer new to C++ and have some questions regarding exception handling. I am using:
    g++ 4.01
    Suse Linux 9
    i486

    Try-Catch blocks seem to work when I issue a throw. However, they do not catch illegal statements such as divide by zero, or segmentation faults. For example,

    Code:
    try{
        throw 1;
    }
    catch (...) {
        cout<<"Exception caught!"<<endl;
    }
    OUTPUT: Exception caught.

    HOWEVER

    Code:
    try{
         int a=1, b=0;
         a=a/b;
    }
    catch (...) {
         cout<<"Exception caught!"<<endl;
    }
    Ends with a program termination "floating point error"

    Am I missunderstanding C++ exception handling, or should the second example be caught? I've tried different complier options (-O0 and -O1), and (-fexceptions) with the same results.

    Thanks,
    Dan
     
    Last edited by a moderator: Mar 25, 2006
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Thats because divide by zero does not throw and exception and you need to test that and throw the exception yourself. Something like
    Code:
    if (b == 0)
        throw 1;
    else
        a=a/b;
     
  3. pacificcoral

    pacificcoral New Member

    Joined:
    Mar 24, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the info.

    So, if I understand you correctly, try-catch can not be used to catch unhandled (un-anticipated) exceptions such as segmentation faults. I was hoping try-catch works similar in C++ as it does in C#, so it can be used to more gracefully handle bugs that would otherwise cause the program to crash.

    Can you please describe what type of exceptions ARE caught from the standard library?

    Dan
     
  4. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    In C++ No exceptions will be caught automatically and you need to be catching them all manually. The C++ in Windows programming catches the segmentation faults and other exception but not the plain Dos based C++.
     
  5. pacificcoral

    pacificcoral New Member

    Joined:
    Mar 24, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    A segmentation fault is difficult to anticipate and protect against. I imagine that g++ in Linux probably generates a signal for this and other type of system errors. How do I go about 'catching' these signals to prevent ugly terminination?
     
  6. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Actually its not that difficult if the design is done correctly but yes its a difficult job if implemented from middle.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice