Click here to Skip to main content
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ Linux
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 8-Feb-13 11:38am
tcnm326
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: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
 
good
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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Web01 | 2.8.160204.4 | Advertise | Privacy
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service

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