Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I fix this warning on CodeBlocks IDE, warning: catching polymorphic type 'class std::domain_error' by value [-Wcatch-value=]

Tags:

c++

warnings

I have a throw domain_error("Student has done no homework") exception in my grade function, if the user does not enter homework grades, causing the vector to be size of 0. then I called this function in the main, within a try and catch block, and the compiler is giving the warning.

Partial view of the main function

read_hw(cin,homework);
try
{
    double _final_grade = grade(midterm,_final,homework);
    streamsize prec = cout.precision();
    cout << "Your final grade is "<<setprecision(3) << _final_grade << 
    setprecision(prec) << endl;
}
catch(domain_error)
{
    cout << "You must enter your grades. Please try again" <<endl;
    return -1;
}
like image 984
user11248708 Avatar asked Mar 23 '19 22:03

user11248708


1 Answers

Exceptions should be caught by constant reference, not by value.

Use

catch(const domain_error &)
like image 183
jkb Avatar answered Oct 21 '22 05:10

jkb