C++ 正确的试捕设计

C++ 正确的试捕设计,c++,try-catch,C++,Try Catch,我如何使这个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 thi

我如何使这个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();

out显然您需要移出
您应该抛出异常,而不是数据,这里根本不需要异常。只需返回
null
。谢谢。奇怪,因为我们被要求使用try-catch并抛出异常。。。另外,我的教授在向我们演示时使用了“继续”。很高兴知道
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;
}