Program executed on Cygwin does not report a thrown exception

心不动则不痛 提交于 2020-02-13 16:29:45

问题


When I run a simple program shown below, I get different terminal output on Cygwin and Ubuntu OS.

#include    <cstdio>
#include    <stdexcept>
#include    <cmath>

using namespace std;

double square_root(double x)
{
    if (x < 0)
        throw out_of_range("x<0");

    return sqrt(x);
}

int main() {
    const double input = -1;
    double result = square_root(input);
    printf("Square root of %f is %f\n", input, result);
    return 0;
}

On Cygwin, unlike Ubuntu, I do not get any message indicating that an exception was thrown. What could be the reason for that? Is there something I need to download for Cygwin so that it deals with exceptions as it is supposed to?

I am using Cygwin version 1.7.30 with GCC 4.9.0 . On Ubuntu, I have version 13.10 with GCC 4.8.1 . I doubt that difference in compilers matters in this case.


回答1:


Since you do not catch the exception, behaviour depends on the implementation/runtime. This seems to be implemented differently for Linux and cygwin.

You should catch the exception yourself, or use something as explained in the answers to this question.




回答2:


The behaviour is not defined for this case - you are relying on the "kindness" of the C++ runtime to issue some text for the "you didn't catch the exception", which the glibc of Linux indeed does, and apparently the Cygwin does not.

Instead, wrap your main code in a try/catch to handle the throw.

int main() {
    try
    {
        const double input = -1;
        double result = square_root(input);
        printf("Square root of %f is %f\n", input, result);
        return 0;
    }
    catch(...)
    {
        printf("Caught exception in main that wasn't handled...");
        return 10;
    }
}

A nice solution, as suggested by Matt McNabb is to "rename main", and do somethting like this:

int actual_main() {
    const double input = -1;
    double result = square_root(input);
    printf("Square root of %f is %f\n", input, result);
    return 0;
}

int main()
{
    try
    {
        return actual_main();
    }
    catch(std::exception e)
    {
         printf("Caught unhandled std:exception in main: %s\n", e.what().c_str());
    }
    catch(...)
    {
         printf("Caught unhandled and unknown exception in main...\n");
    }
    return 10;
}

Note that we return a different value than zero to indicate "failure" - I expect that at the very least, Cygwin does that already.




回答3:


One way to debug this type of C++ error is by simply just rewriting it in C, then translating it back into C++. C is simpler so translating it to C should eliminate your problem.



来源:https://stackoverflow.com/questions/24402412/program-executed-on-cygwin-does-not-report-a-thrown-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!