Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16943

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Not stopping in exceptions with try catch Posted by tijoen on 4 Jul 2007 at 5:45 PM
Hi, i built this only for testing purpose:

#include <stdio.h>
#include <iostream>

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...
Report
Re: Not stopping in exceptions with try catch Posted by freelance star on 5 Jul 2007 at 4:35 AM
: Hi, i built this only for testing purpose:
:
: #include <stdio.h>
: #include <iostream>
:
: 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.
Freelance Programmer|Interview Question And Answer
Report
Re: Not stopping in exceptions with try catch Posted by Lundin on 5 Jul 2007 at 4:38 AM
: 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.
Report
Re: Not stopping in exceptions with try catch Posted by tijoen on 5 Jul 2007 at 7:17 AM
: 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
Report
Re: Not stopping in exceptions with try catch Posted by Lundin on 5 Jul 2007 at 11:17 PM
: : 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:

ErrorCode func ()
{
...
  return someErrorCode;
}



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.
Report
Re: Not stopping in exceptions with try catch Posted by tracer42 on 28 Nov 2012 at 3:11 PM
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.

Report
Re: Not stopping in exceptions with try catch Posted by tracer42 on 28 Nov 2012 at 3:14 PM
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.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.