c++ - Exception or Error Messages -
this question has answer here:
- c++ - arguments exceptions on return codes 11 answers
i having dilemma in designing function returns double value.
the function performing calculation , designed follows:
double simpsonintegration(const function& f, float a, float b,int n,std::string& errormessages) if(n%2!=0) { errormessages=errormessages+"simpson integration: number of subintervals must number\n"; }else if(n<20||n>100) { errormessages=errormessages+"simpson integration: number of subintervals should between 20 100\n"); } double h=0,x=0,y=0,retval=0;
my dilemma if there error not want proceed double h=0 .... line , want return function asap. however, since designed return double, can not use return expression. have thought using goto statement go end of function or throw errors instead of using error message string. if throw error , forget catch it, terminate program , unfortunately c++ not have mechanism remind programmer function throws error , must caught caller.
what type of design recommend handle errors? thanking in advance.
if error must not ignored - use throw-catch.
if error can ignored (doesn't seem case though)you have 2 options:
- use separate pass-by-reference parameter error code
- return error code (or code
success
) , use pass-by-reference parameter return actual result
Comments
Post a Comment