[英]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. 我是C ++的初学者,并试图创建一个简单的控制台程序来计算线性方程的'm'和'b'...来解析用户提供的输入双,我使用的是字符串流并尝试使用-catch块检查错误输入。 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..]
即使catch块具有全局异常,持久性错误也会继续跟踪[方程Solver.exe中的0x74c8b9bc处的未处理异常:Microsoft C ++异常:[rethrow]在内存位置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. 没有参数的
throw
只能在处理异常时使用(即在catch块或直接或间接从catch块调用的函数中),否则你必须使用带有参数的throw,这通常是一个异常对象某种。
If throw
is executed when an exception is not being handled std::terminate
will be called to end your program. 如果在未处理异常时执行
throw
将调用std::terminate
来结束程序。
eg (after #include <stdexcept>
) 例如(在
#include <stdexcept>
)
throw std::runtime_error("Bad input");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.