C++;Try-Catch块未捕获异常 我是C++初学者,尝试创建一个简单的控制台程序来计算线性方程的M’和B’…为了解析用户提供的双精度输入,我使用stringstream和try-catch块来检查错误输入。即使catch块有一个全局异常(在0x7C8B9BC中未处理的异常,在方程SoVel.EX:微软C++异常:[重新抛出]在内存位置0x00亿…..)/P> 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(); doublexone`在这里输入代码` 双音; 双XTwo; 双钇; 布尔输入检查=假; while(inputCheck==false) { 清除(); 写线(“**线性方程**”); 写(“X1:”); 字符串xone=ReadLine(); 写(“Y1:”); 字符串yone=ReadLine(); 写(“X2:”); 字符串xtwo=ReadLine(); 写(“Y2:”); 字符串ytwo=ReadLine(); 尝试 { 串流s1(xone); if(s1>>XOne){s1>>XOne;}else{throw;} SrimSnfsS2(yOne);//考虑我给这个变量提供无效输入 如果(s2>>YOne){s2>>YOne;}或者{throw;}//这并不能解决问题 stringstreams3(xtwo); if(s3>>XTwo){s3>>XTwo;}else{throw;} 串流s4(ytwo); if(s4>>YTwo){s4>>YTwo;}else{throw;} } catch(…){WriteLine(“无效输入”);ReadLine();} } 线性回归方程; 方程初始化(XOne,YOne,XTwo,YTwo); stringstream s5; s5 XOne){s1>>XOne;}否则{抛出运行时错误(“无效输入”);} stringstream s2(yone); if(s2>>YOne){s2>>YOne;}else{throw runtime_error(“无效输入”);} stringstreams3(xtwo); if(s3>>XTwo){s3>>XTwo;}else{throw runtime_error(“无效输入”);} 串流s4(ytwo); if(s4>>YTwo){s4>>YTwo;}else{throw runtime_error(“无效输入”);} } catch(运行时错误e){WriteLine(e.what());ReadLine();} } 线性回归方程; 方程初始化(XOne,YOne,XTwo,YTwo); stringstream s5; s5
C++;Try-Catch块未捕获异常 我是C++初学者,尝试创建一个简单的控制台程序来计算线性方程的M’和B’…为了解析用户提供的双精度输入,我使用stringstream和try-catch块来检查错误输入。即使catch块有一个全局异常(在0x7C8B9BC中未处理的异常,在方程SoVel.EX:微软C++异常:[重新抛出]在内存位置0x00亿…..)/P> 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(); doublexone`在这里输入代码` 双音; 双XTwo; 双钇; 布尔输入检查=假; while(inputCheck==false) { 清除(); 写线(“**线性方程**”); 写(“X1:”); 字符串xone=ReadLine(); 写(“Y1:”); 字符串yone=ReadLine(); 写(“X2:”); 字符串xtwo=ReadLine(); 写(“Y2:”); 字符串ytwo=ReadLine(); 尝试 { 串流s1(xone); if(s1>>XOne){s1>>XOne;}else{throw;} SrimSnfsS2(yOne);//考虑我给这个变量提供无效输入 如果(s2>>YOne){s2>>YOne;}或者{throw;}//这并不能解决问题 stringstreams3(xtwo); if(s3>>XTwo){s3>>XTwo;}else{throw;} 串流s4(ytwo); if(s4>>YTwo){s4>>YTwo;}else{throw;} } catch(…){WriteLine(“无效输入”);ReadLine();} } 线性回归方程; 方程初始化(XOne,YOne,XTwo,YTwo); stringstream s5; s5 XOne){s1>>XOne;}否则{抛出运行时错误(“无效输入”);} stringstream s2(yone); if(s2>>YOne){s2>>YOne;}else{throw runtime_error(“无效输入”);} stringstreams3(xtwo); if(s3>>XTwo){s3>>XTwo;}else{throw runtime_error(“无效输入”);} 串流s4(ytwo); if(s4>>YTwo){s4>>YTwo;}else{throw runtime_error(“无效输入”);} } catch(运行时错误e){WriteLine(e.what());ReadLine();} } 线性回归方程; 方程初始化(XOne,YOne,XTwo,YTwo); stringstream s5; s5,c++,exception,error-handling,try-catch,C++,Exception,Error Handling,Try Catch,throw只有在处理异常时(即在catch块或从catch块直接或间接调用的函数中),才可以使用不带参数的throw,否则必须将throw与通常是某种异常对象的参数一起使用 如果在未处理异常时执行了throw,std::terminate将被调用以结束程序 e、 g.(在之后#包括) double XOne; double YOne; double XTwo; double YTwo; bool inputCheck = false; while (inputCheck == false) {
throw
只有在处理异常时(即在catch块或从catch块直接或间接调用的函数中),才可以使用不带参数的throw,否则必须将throw与通常是某种异常对象的参数一起使用
如果在未处理异常时执行了throw
,std::terminate
将被调用以结束程序
e、 g.(在之后#包括)
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 std::runtime_error("Bad input");