c++ubuntugcccygwinoutofrangeexception

Program executed on Cygwin does not report a thrown exception


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.


Solution

  • 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.