如何才能使try-catch不会输出两次错误?下面的代码仅用于识别snap、csg、cdh和cr。它像这样打印出来:
Input Strings:
snap(12345,Charlie Brown,Manager,555-1234).
snap(67890,Lucy,Right Field,555-5678).
csg(CS101,12345,A).
csg(CS101,67890,B).
csgs(CS101,67890,B). // i want to get rid of this
**Error: csgs(CS101,67890,B). // repeat
cdh(CS101,M,9AM).
cr(CS101,1170 TMCB).下面是我的代码:
out << "Input Strings:" << endl;
for (string line; getline(in, line);) {
out << line;
try {
if ("snap(" == line.substr(0, 5)) {
string studentID = line.substr(5, line.find(',') - 5);
line = line.substr(line.find(',') + 1);
string studentName = line.substr(0, line.find(','));
line = line.substr(line.find(',') + 1);
string studentAddress = line.substr(0, line.find(','));
line = line.substr(line.find(',') + 1);
string studentPhone = line.substr(0, line.find(')'));
snaps.emplace_back(studentID, studentName, studentAddress, studentPhone);
continue;
}
else if ("csg(" == line.substr(0, 4)) {
string courseName = line.substr(4, line.find(',') - 4);
line = line.substr(line.find(',') + 1);
string studentID = line.substr(0, line.find(','));
line = line.substr(line.find(',') + 1);
string studentGrade = line.substr(0, line.find(')'));
csg.emplace_back(courseName, studentID, studentGrade);
continue;
}
else if ("cdh(" == line.substr(0, 4)) {
string courseName = line.substr(4, line.find(',') - 4);
line = line.substr(line.find(',') + 1);
string courseDay = line.substr(0, line.find(','));
line = line.substr(line.find(',') + 1);
string courseTime = line.substr(0, line.find(')'));
cdh.emplace_back(courseName, courseDay, courseTime);
continue;
}
else if ("cr(" == line.substr(0, 3)) {
string courseName = line.substr(3, line.find(',') - 3);
line = line.substr(line.find(',') + 1);
string courseRoom = line.substr(0, line.find(')'));
cr.emplace_back(courseName, courseRoom);
continue;
}
throw string(line);
}
catch (const runtime_error& error) { out << endl << "**Error: " << error.what(); }
catch (const string& e) { out << endl << "**Error: " << e; }
}
in.close();发布于 2020-09-26 14:11:30
显然,您需要移动out << line;,以便只有在您知道没有错误时才打印它。
我看不出在这段代码中需要异常。异常真的应该为异常情况保留,跳过坏数据并不是真的那样。此外,continue语句通常被认为是不好的风格,同样是因为它们会中断程序的正常流程,使其难以理解。
这是没有异常和没有continue的重写代码。您真正需要做的就是处理else子句中的错误情况,并将行的打印移动到循环的末尾。
out << "Input Strings:" << endl;
for (string line; getline(in, line);) {
if ("snap(" == line.substr(0, 5)) {
string studentID = line.substr(5, line.find(',') - 5);
line = line.substr(line.find(',') + 1);
string studentName = line.substr(0, line.find(','));
line = line.substr(line.find(',') + 1);
string studentAddress = line.substr(0, line.find(','));
line = line.substr(line.find(',') + 1);
string studentPhone = line.substr(0, line.find(')'));
snaps.emplace_back(studentID, studentName, studentAddress, studentPhone);
}
else if ("csg(" == line.substr(0, 4)) {
string courseName = line.substr(4, line.find(',') - 4);
line = line.substr(line.find(',') + 1);
string studentID = line.substr(0, line.find(','));
line = line.substr(line.find(',') + 1);
string studentGrade = line.substr(0, line.find(')'));
csg.emplace_back(courseName, studentID, studentGrade);
}
else if ("cdh(" == line.substr(0, 4)) {
string courseName = line.substr(4, line.find(',') - 4);
line = line.substr(line.find(',') + 1);
string courseDay = line.substr(0, line.find(','));
line = line.substr(line.find(',') + 1);
string courseTime = line.substr(0, line.find(')'));
cdh.emplace_back(courseName, courseDay, courseTime);
}
else if ("cr(" == line.substr(0, 3)) {
string courseName = line.substr(3, line.find(',') - 3);
line = line.substr(line.find(',') + 1);
string courseRoom = line.substr(0, line.find(')'));
cr.emplace_back(courseName, courseRoom);
}
else {
out << endl << "**Error: ";
}
out << line << endl;
}https://stackoverflow.com/questions/64074275
复制相似问题