Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (1 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.

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.
 
Share this answer
 
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900