简体   繁体   English

从另一个函数调用的函数向main抛出错误

[英]Throwing an error to main from a function called by another function

Let's say I have a function ( mnHw ) that calculates the mean of a vector (a vector of homework grades). 假设我有一个函数( mnHw ),可计算向量(作业成绩的向量)的平均值。 This function throws a domain_error("Student did no homework") exception when the vector is empty. 当向量为空时,此函数将引发domain_error("Student did no homework")异常。 When I call mnHw from main , things are simple when I want to print out the error: 当我从main调用mnHw时,要打印错误时事情很简单:

        try
        {
            cout  <<  mnHw(student.homework);
        }
        catch (domain_error e)
        {
            cout << e.what();
        }

However, things are complicated if I only want to store the mean homework grade, instead of vector of all grades, for the student. 但是,如果我只想存储学生的平均作业成绩而不是所有成绩的向量,那么事情就很复杂。 In that case I call mnHw within the function that reads in the student information ( readStudent ), and put up a flag (-1) when no homework is entered: 在那种情况下,我在读取学生信息( readStudent )的函数内调用mnHw ,并在未输入任何作业的情况下设置一个标志(-1):

        try
        {
            student.finalGrade=mnHw(hwGrades);
        }
        catch (domain_error e)
        {
            student.finalGrade = -1;
        }

Then, within main , I can do the following to recover the error: 然后,在main ,我可以执行以下操作来恢复错误:

    if (allStudents[i].finalGrade == -1) 
        cout << "Student did no homework";
    else  
        cout << allStudents[i].finalGrade;

Intuitively, though, this flag method seems less elegant than having the actual error message pass directly to main, at least for those cases when I want to display it from main (which is the case here). 但是,从直觉上讲,这种标志方法似乎不如直接将实际错误消息传递给main那样优雅,至少在我要从main显示它的情况下(在这里就是这种情况)。 Is there some trick or technique I am missing that would directly give main access to e.what() from mnHw ? 是有一些技巧或技术,我很想念这将直接给main访问e.what()mnHw

What is good practice? 什么是好习惯?

Note I have full functions for each of these cases that I could post, but they seemed too long. 请注意,我可以针对每种情况提供完整的功能,但是它们似乎太长了。 Please let me know if I should have included them to make myself more clear. 请让我知道我是否应该包括它们,以使自己更加清楚。 Also, please feel free to correct any other mistakes I'm making. 另外,请随时纠正我犯的任何其他错误。 I am just now learning C++ the right way, and want to be corrected as much as possible. 我现在正以正确的方式学习C ++,并且希望得到尽可能多的纠正。 <flame-retardant vest>

You can re-throw the exception catched (const domain_error* e){ throw e;} . 您可以重新引发捕获的异常(const domain_error* e){ throw e;} Or better, you can create based on domain_error create another exception ie, student_final_grade_exception and throw it. 或者更好的是,您可以基于domain_error创建另一个异常,即student_final_grade_exception并将其抛出。

EDIT: 编辑:

Something like: 就像是:

 try
        {
            student.finalGrade=mnHw(hwGrades);
        }
        catch (domain_error& e)
        {
              student_final_grade_exception mean_excep;
              mean_excep.addInfo("Error in calculation means...");
              mean_excep.addInfo(e.what()); 
              throw mean_excep;
        }

And then, you prepare your main to catch student_final_grade_exception . 然后,准备您的main以捕获student_final_grade_exception Of course, there is a litle bit more work to create a new exception type, but it can make possible to get more info and let the functions to only what they suppose to do as were told. 当然,创建新的异常类型还需要做更多的工作,但是它可以获取更多信息,并使函数仅按要求执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM