try/catch/throw exceptions
Hello,
I have a program where I am calculating a number and then taking the inverse of it (i.e: 1/x), I am going through this calculation many times...but there are times when x=0, which will make it not possible to take the invese of(i.e 1/0)......I am attempting to use a try /catch/throw method....my function's name is solver, and it is being called in main:
int main(){
try { solver(); }
}
now the function:
void solver(){
//this is where my ? is, I just need to check is if x == 0??
if (x == 0){
//is this exception, or do I write invalid_argument or range_error
//or is it basically anything I want it to be, are they biult in?
throw_exception("x found to be 0";)
}
//now back to main
int main() {
try { solver(); }
catch(exception ){
cout<<"error, 0 calculated<<endl;}//end catch
}
I am getting a many errors with this...and since I've never used try/catch/throw...I am not sure what I am doing wrong...
also, when it does catch an exception, does it exit the program?
or is there a way to catch the exception and go to the next iteration and next calculation?? thanks for any help...
having problems with try/catch throw
hello...
I'm having some trouble figuring out why my code is not catching exceptions...
I have tried the syntax suggested above...but nothing is getting printed to the screen about getting the 0...this is the code:
int main(){
makesolver sheet(); }
void makesolversheet() {
get();
bestfit(); //this is the function that is calculating a slope value
}//end makesolversheet
void bestfit() {
--some calculations--
if (slope == 0){
try { tryslope(slope); }//if slope is 0
}//end try
catch (char* a) {
cout <<a<<'\n'; } //end catch
}//end bestfit
void tryslope(float x) {
if (x == 0)
{ throw ("slope calc. to be 0"); }
}//end tryslope
I have assigned in my code for slope to be 0 so that it enters the loop in the bestfit function and tryslope gets called...however when I run it...the statement: "slope calc to be 0" is not showing up... ( My main need is to be able to go through many calculaiton of the slope...when 0 is caluculated...let the user know and move on to the next calculation...not exiting the program)...however this is not even printing that 0 was calc...any help will be greatly appreciated... thanks