Catching exception: divide by zero

后端 未结 8 1643
时光取名叫无心
时光取名叫无心 2020-11-22 16:23

The following code does not catch an exception, when I try to divide by 0. Do I need to throw an exception, or does the computer automatically throw one at runtime?

8条回答
  •  心在旅途
    2020-11-22 17:22

    Updated with comments from ExcessPhase

    GCC (at least version 4.8) will let you emulate this behaviour:

    #include 
    #include 
    #include 
    
    int main() {
        std::shared_ptr handler(
            signal(SIGFPE, [](int signum) {throw std::logic_error("FPE"); }),
            [](__sighandler_t f) { signal(SIGFPE, f); });
    
        int i = 0;
    
        std::cin >> i;  // what if someone enters zero?
    
        try {
            i = 5/i;
        }
        catch (std::logic_error e) {
            std::cerr << e.what();
        }
    }
    

    This sets up a new signal handler which throws an exception, and a shared_ptr to the old signal handler, with a custom 'deletion' function that restores the old handler when it goes out of scope.

    You need to compile with at least these options:

    g++ -c Foo.cc -o Foo.o -fnon-call-exceptions -std=c++11
    

    Visual C++ will also let you do something similar:

    #include 
    #include 
    
    int main() {
        std::shared_ptr handler(
            _set_se_translator([](unsigned u, EXCEPTION_POINTERS* p) {
                switch(u) {
                    case FLT_DIVIDE_BY_ZERO:
                    case INT_DIVIDE_BY_ZERO:
                        throw std::logic_error("Divide by zero");
                        break;
                    ...
                    default:
                        throw std::logic_error("SEH exception");
                }
            }),
            [](_se_translator_function f) { _set_se_translator(f); });
    
        int i = 0;
    
        try {
            i = 5 / i;
        } catch(std::logic_error e) {
            std::cerr << e.what();
        }
    }
    

    And of course you can skip all the C++11-ishness of this and put them in a traditional RAII-managing struct.

提交回复
热议问题