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)
                "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];
                        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.
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With