Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Not stopping in exceptions with try catch

tijoentijoen Posts: 141Member
Hi, i built this only for testing purpose:

#include
#include

using namespace std;


int main(int argc, char *argv[]){

try
{
int z = 3 / 0;
}
catch (...){
cout << "TEST!!!";
}
return 0;
}

Shouldn't it stop in the catch and print "TEST!!!" ?

because it didn't... when i run it, it appears to me "floating point exception"
i am using this command to build it in my centos box:
g++ -g -o test test.cpp
it prints a warning
test.cpp:11: warning: division by zero in `3 / 0'

this is really bugging me out...
it only goes to the catch if I throw something

any help is appreciated...

Comments

  • freelance starfreelance star Posts: 73Member
    : Hi, i built this only for testing purpose:
    :
    : #include
    : #include
    :
    : using namespace std;
    :
    :
    : int main(int argc, char *argv[]){
    :
    : try
    : {
    : int z = 3 / 0;
    : }
    : catch (...){
    : cout << "TEST!!!";
    : }
    : return 0;
    : }
    :
    : Shouldn't it stop in the catch and print "TEST!!!" ?
    :
    : because it didn't... when i run it, it appears to me "floating point
    : exception"
    : i am using this command to build it in my centos box:
    : g++ -g -o test test.cpp
    : it prints a warning
    : test.cpp:11: warning: division by zero in `3 / 0'
    :
    : this is really bugging me out...
    : it only goes to the catch if I throw something
    :
    : any help is appreciated...
    who said 3/0 throws an exception, hardware terminates your program for giving illegal instruction.
    [B][link=http://expert.myitcareer.org/]Freelance Programmer[/link]|[link=http://myitcareer.org/]Interview Question And Answer[/link][/B]
  • LundinLundin Posts: 3,711Member
    : Shouldn't it stop in the catch and print "TEST!!!" ?

    No, C++ isn't Java :-)

    It doesn't catch anything not thrown. Though if you are using a RAD tool like Borland Builder, they have their own set of exception classes and such.


    : because it didn't... when i run it, it appears to me "floating point
    : exception"

    That exception was thrown by Windows itself, not your program.
  • tijoentijoen Posts: 141Member
    : No, C++ isn't Java :-)

    then why does exist try catch blocks?
    please, can you show me a good way to use exceptions?
    using classes of exception or not
  • LundinLundin Posts: 3,711Member
    : : No, C++ isn't Java :-)
    :
    : then why does exist try catch blocks?
    : please, can you show me a good way to use exceptions?
    : using classes of exception or not
    :


    They exist for error handling of your own program. In C, the traditional way to do error handling in function is like this:

    [code]ErrorCode func ()
    {
    ...
    return someErrorCode;
    }[/code]


    The problem with the above is that you'll have to return values calculated in the function through pointers. If the error code is passed with an exception instead, the function can use the return value for actual values.

    And imagine an operator overloading:

    SomeClass operator = (SomeClass& obj);

    All the parameters and the return value must be in a fixed pattern. Without exception handling, you wouldn't be able to handle errors from that function.
  • tracer42tracer42 Posts: 2Member
    Even though this is very old, I thought I would reply since none of the existing replies seem to be correct.

    The fpu will trigger a hardware exception on divide-by-zero if the exception is unmasked in the fpu control register (see _control87() or _controlfp()). When the exception is raised, Windows turns it into a SEH (Structured Exception Handler)exception, which is compatible with Microsoft C and is caught using the __try .. __except mechanism. C++ exceptions, using try {} .. catch {} are a different mechanism. The catch (...) statement will only catch SEH exceptions if you tell the compiler that you want to do so. In Visual C++ 2010, go to project properties / Configuration Properties / C/C++ / Code Generation / Enable C++ Exceptions and select "Yes with SEH Exceptions (/EHa)". Then, if a floating point exception is generated, it should be caught by the C++ "catch (...)" statement.

    Another problem that can cause confusion is that fpu exceptions are not actually generated until the next floating point operation or fwait instruction. In Visual C++ 2010, if you select "Yes /fp:except" for the "Enable Floating Point Exceptions" option, exceptions will be raised immediately after they are triggered.

  • tracer42tracer42 Posts: 2Member
    Even though this is very old, I thought I would reply since none of the existing replies seem to be correct.

    The fpu will trigger a hardware exception on divide-by-zero if the exception is unmasked in the fpu control register (see _control87() or _controlfp()). When the exception is raised, Windows turns it into a SEH (Structured Exception Handler)exception, which is compatible with Microsoft C and is caught using the __try .. __except mechanism. C++ exceptions, using try {} .. catch {} are a different mechanism. The catch (...) statement will only catch SEH exceptions if you tell the compiler that you want to do so. In Visual C++ 2010, go to project properties / Configuration Properties / C/C++ / Code Generation / Enable C++ Exceptions and select "Yes with SEH Exceptions (/EHa)". Then, if a floating point exception is generated, it should be caught by the C++ "catch (...)" statement.

    Another problem that can cause confusion is that fpu exceptions are not actually generated until the next floating point operation or fwait instruction. In Visual C++ 2010, if you select "Yes /fp:except" for the "Enable Floating Point Exceptions" option, exceptions will be raised immediately after they are triggered.

Sign In or Register to comment.