Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot catch and handle floating point exception? [duplicate]

Tags:

c++

I try to write a simple program to practicing Expection of C++, but I cannot catch and handle floating point exception?

This is my code.

#include <iostream>                                              
#include <string>                                                
#include <exception>                                             

using namespace std;                                             

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

    int num_1[] = {0, 2, 4, 6, 8}, num_2[] = {3, 2, 1, 0, -1};   

    for(int i = 0; i < sizeof(num_1) / sizeof(num_1[0]); i++) {  
        try {                                                    
            int result = num_1[i] / num_2[i];                    
            printf("%d / %d = %d\n", num_1[i], num_2[i], result);
        } catch(exception &e) {                                  
            cout << e.what() << endl;                            
            cout << "something is wrong." << endl;               
            continue;                                            
        }                                                        
    }                                                            

    return 0;                                                    
}                                                                

This is my result, but is not I want.

0 / 3 = 0
2 / 2 = 1
4 / 1 = 4
Floating point exception (core dumped)
like image 312
Graycat Avatar asked Jun 24 '19 04:06

Graycat


Video Answer


2 Answers

"Floating point exception" is the name of a signal (SIGFPE). You get this signal if you try to divide an integer by 0 (or if you divide INT_MIN by -1). In other words, it has nothing to do with floating point operations or exceptions (in the C++ sense), so the name is a bit unfortunate.

The easiest solution is to check for 0 beforehand:

if (num_2[i] == 0 || (num_1[i] == INT_MIN && num_2[i] == -1)) {
    // handle error, or throw your own exception
    ...
}
int result = num_1[i] / num_2[i];
like image 110
melpomene Avatar answered Nov 22 '22 00:11

melpomene


Thanks everyone, I understand my mistake with your help. And I am sorry that I can't reply to every comment. Please allow me sort out helpful answer.

  • "Floating point exception" is the name of a signal instead of exception.
  • "Integer divide by zero" is not an exception in standard C++.

This my new code.

#include <iostream>                                                                       
#include <exception>                                                                      

using namespace std;                                                                      

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

    int num_1[] = {0, 2, 4, 6, 8}, num_2[] = {3, 2, 1, 0, -1};                            

    for(int i = 0; i < sizeof(num_1) / sizeof(num_1[0]); i++) {                           
        try {                                                                             
            if(num_2[i] == 0 || (num_1[i] == INT32_MIN && num_2[i] == -1)) {              
                throw "catch my expection";                                               
            }                                                                             
            cout << num_1[i] << " / " << num_2[i] << " = " << num_1[i] / num_2[i] << endl;
        } catch(const char *e_msg) {                                                      
            cout << e_msg << endl;                                                        
            cout << "sth is error" << endl;                                               
            continue;                                                                     
        }                                                                                 
    }                                                                                     

    return 0;                                                                             
}                                                                                         

This my result which is I want.

0 / 3 = 0
2 / 2 = 1
4 / 1 = 4
catch my expection
sth is error
8 / -1 = -8

like image 22
Graycat Avatar answered Nov 22 '22 01:11

Graycat