简体   繁体   中英

C++ Try Catch Block does not catch an exception

I am a beginner in C++ and trying to create a simple console program that calculates the 'm' and 'b' of a linear equation... to parse the input double that the user provides, I am using a stringstream and using a try-catch block to check for false inputs. A persistent error keeps following even though the catch block has a global exception [Unhandled exception at 0x74c8b9bc in Equation Solver.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..]

double XOne;`enter code here`
double YOne;
double XTwo;
double YTwo;
bool inputCheck = false;
while (inputCheck == false)
{
    Clear();
    WriteLine("**Linear Equation**");
    Write("X1: ");
    string xone = ReadLine();
    Write("Y1: ");
    string yone = ReadLine();
    Write("X2: ");
    string xtwo = ReadLine();
    Write("Y2: ");
    string ytwo = ReadLine();
    try
    {
        stringstream s1(xone);
        if (s1 >> XOne) { s1 >> XOne; } else { throw; }
        stringstream s2(yone); // consider I give an invalid input for this variable
        if (s2 >> YOne) { s2 >> YOne; } else { throw; } // this doesn't solve the problem
        stringstream s3(xtwo);
        if (s3 >> XTwo) { s3 >> XTwo; } else { throw; }
        stringstream s4(ytwo);
        if (s4 >> YTwo) { s4 >> YTwo; } else { throw; }
    }
    catch (...) { WriteLine("Invalid Input"); ReadLine(); }
}

LinearEquation equation;
equation.Initialize(XOne, YOne, XTwo, YTwo);
stringstream s5;
s5 << equation.m;
string m = s5.str();
stringstream s6;
s6 << equation.b;
string b = s6.str();
Write("Y = ");
Write(m);
Write("X + ");
WriteLine(b);
ReadLine();

EDIT The first suggestion worked like a charm... Thank you! This is the code after I modified it according to the reviewer.

double XOne;
double YOne;
double XTwo;
double YTwo;
bool inputCheck = false;
while (inputCheck == false)
{
    Clear();
    WriteLine("**Linear Equation**");
    Write("X1: ");
    string xone = ReadLine();
    Write("Y1: ");
    string yone = ReadLine();
    Write("X2: ");
    string xtwo = ReadLine();
    Write("Y2: ");
    string ytwo = ReadLine();
    try
    {
        stringstream s1(xone);
        if (s1 >> XOne) { s1 >> XOne; } else { throw runtime_error("Invalid Input"); }
        stringstream s2(yone);
        if (s2 >> YOne) { s2 >> YOne; } else { throw runtime_error("Invalid Input"); }
        stringstream s3(xtwo);
        if (s3 >> XTwo) { s3 >> XTwo; } else { throw runtime_error("Invalid Input"); }
        stringstream s4(ytwo);
        if (s4 >> YTwo) { s4 >> YTwo; } else { throw runtime_error("Invalid Input"); }
    }
    catch (runtime_error e) { WriteLine(e.what()); ReadLine(); }
}

LinearEquation equation;
equation.Initialize(XOne, YOne, XTwo, YTwo);
stringstream s5;
s5 << equation.m;
string m = s5.str();
stringstream s6;
s6 << equation.b;
string b = s6.str();
Write("Y = ");
Write(m);
Write("X + ");
WriteLine(b);
ReadLine();

throw without an argument can only be used when there is an exception being handled (ie in a catch block or a function called directly or indirectly from a catch block), otherwise you have to use throw with an argument which is usually an exception object of some sort.

If throw is executed when an exception is not being handled std::terminate will be called to end your program.

eg (after #include <stdexcept> )

throw std::runtime_error("Bad input");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM