- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是 C++ 的初学者,我正在尝试创建一个简单的控制台程序来计算线性方程的“m”和“b”...为了解析用户提供的 double 输入,我使用的是字符串流和使用 try-catch block 检查错误输入。即使 catch block 有全局异常 [Equation Solver.exe 中 0x74c8b9bc 处未处理的异常:Microsoft C++ 异常:内存位置 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();
编辑第一个建议就像一个魅力......谢谢!这是我按照审稿人修改后的代码。
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
没有参数只能在处理异常时使用(即在 catch block 或从 catch block 直接或间接调用的函数中),否则你必须使用 throw 和一个通常是某些异常对象的参数排序。
如果throw
在未处理异常时执行std::terminate
将被调用以结束您的程序。
例如(在 #include <stdexcept>
之后)
throw std::runtime_error("Bad input");
关于C++ Try Catch block 不捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11872349/
我刚刚遇到了一个非常奇怪的行为。这是代码: // So far everything's fine val x: Try[Try[Unit]] = Try(Try{}) x: scala.util.T
“输出”是一个序列化的 OpenStruct。 定义标题 try(:output).try(:data).try(:title) 结束 什么会更好? :) 最佳答案 或者只是这样: def title
我有以下元组 - (t1,t2) :(Try,Try) 我想检查两者是否成功或其中之一是否失败,但避免代码重复。像这样的东西: (t1,t2) match { case (Success(v1),Su
是否必须放置内部 try-with-resources 或其中一个 try-with-resources 中的所有内容都会自动关闭? try (BasicDataSource ds = Bas
有一点特殊,尝试创建一段 try catch 代码来处理 GoogleTokenResponse,但编译器在 try 时抛出异常错误。有什么想法吗? 错误信息: | Loading Grails 2.
它几乎可以在所有语言中找到,而且我大部分时间都在使用它。 我不知道它是内部的,不知道它是如何真正起作用的。 它如何在任何语言的运行时在 native 级别工作? 例如:如果在 try 内部发生 sta
为什么在 readFile2() 中我需要捕获 FileNotFoundException 以及稍后由 close( ) 方法,并且在 try-with-resources(inside readfi
我正在使用 Apache POI 尝试读取 Word 文件,但即使您使用过 Apache POI,这仍然应该是可以回答的。在 HWPF.extractor 包中有两个对象:WordExtractor
如果try-catch的catch block 中抛出异常,那么finally block 会被调用吗? try { //some thing which throws error } cat
这个问题已经有答案了: What's the purpose of try-with-resources statements? (7 个回答) 已关闭 3 年前。 我一直在查看代码,并且已经看到了对
这个问题已经有答案了: What's the purpose of try-with-resources statements? (7 个回答) 已关闭 3 年前。 我一直在查看代码,并且已经看到了对
我正在使用 Try::Tiny尝试捕捉。 代码如下: use Try::Tiny; try { print "In try"; wrongsubroutine(); # undefi
我想知道这样的代码是否会在抛出异常后总是中断而不继续运行,因此代码不会继续执行第二个 temp.dodaj(b)。 Avto *a = new Avto("lambo",4); Avt
我知道在try子句中必须有一个与资源关联的变量声明。 但是除了被分配一个通常的资源实例化之外,它是否可以被分配一个已经存在的资源,例如: public String getAsString(HttpS
我有一个写的方法。此方法仅扫描用户输入的整数输入。如果用户输入一个字符值,它将抛出一个输入不匹配异常,这是在我的 Try-Catch 语句中处理的。问题是,如果用户输入任何不是数字的东西,然后抛出异常
我注意到这不会编译: PrintWriter printWriter = new PrintWriter("test.txt"); printWriter.append('a'); printWrit
我经常看到人们写这样的代码: try: some_function() except: print 'something' 当我认为这样做更干净时: try: some_functio
该应用程序将在第二个显示器上正常显示内容。问题是当我旋转 iPad 时内容不会在 iPad 上旋转。 看过: http://developer.apple.com/library/ios/#qa/qa
我正在学习 java,我发现我不喜欢的一件事通常是当我有这样的代码时: import java.util.*; import java.io.*; public class GraphProblem
我使用 C++ 有一段时间了,对普通的 try/catch 很熟悉。但是,我现在发现自己在 Windows 上,在 VisualStudio 中编码以进行 COM 开发。代码的几个部分使用了如下内容:
我是一名优秀的程序员,十分优秀!