A program running on Cygwin does not report an exception

When I run the simple program shown below, I get different 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;
}

In Cygwin, unlike Ubuntu, I get no message saying that an exception was thrown. What could be the reason for this? Is there anything 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 the difference in compilers matters in this case.

+4
source share
2 answers

, / . , - Linux cygwin.

-, .

+3

- "" ++, - " ", glibc Linux, , -, Cygwin .

try/catch 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;
    }
}

, , - " " - :

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;
}

, , , "" - , , , Cygwin .

+6

Source: https://habr.com/ru/post/1545942/


All Articles