Click here to Skip to main content
14,542,548 members
Rate this:
Please Sign up or sign in to vote.
See more:
I understood that catch (...) was supposed to catch any exception. In the following program, however, catch (...) fails to catch a floating point (divide by zero) exception.
#include <iostream>
using namespace std;

int main () 
  {
    int top = 90;
    int bottom = 0;
    
    try
      {
        cout << "top / 2 = " << top / 2 << endl;
        cout << "top / bottom = " << top / bottom << endl;
      }
    catch ( ... ) 
      {
        cout << "We have a problem." << endl;
      }
    cout << "Done" << endl;
    return (0);
  }

Has the C++ specification changed, or is something wrong with the program?

Thank you very much,

TCNM
Posted
Comments
tcnm 8-Feb-13 16:51pm
   
Thanks very much.

TCNM
H.Brydon 8-Feb-13 23:34pm
   
I see that the question has already been answered to your satisfaction but I would like to point out that this is not a floating point divide by zero. The values you use here are all int, and you are producing an integer divide by zero. this is not a floating point exception.
Rate this:
Please Sign up or sign in to vote.

Solution 2

If you're using Visual Studio there is s build option to turn on/off floating point exceptions Configuration Properties\C/C++\Code Generation which will at least get it to throw ( adds /fp:except to comand line ). Exactly what you'd have to catch I'm not sure but catch(...) should work. As Victor indicates though it's not standard so not portable.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100