Exception Handler Crashes

I am trying to add an exception handler for Dividing by zero, however if the if is TRUE, it crashes the program. any help would be appreciated

Here is the snippet that I am getting the error at..

mid4 = mid1-1;//this is the sum of the previous formula times number of months minus 1
{
if (mid4 == 0)// Checks the mid4 int to deduce whether it is zero or not.
throw std::overflow_error("Divide by zero exception");
}
mid5 = mid2 / mid4;// this is the figuring of the mortgage amount
You throw an exception, but (I assume) you never catch it, so why wouldn't your program crash?
Thank you, I added the Catch... but still crashing. I have to admit, I am a little confused on how this is processing. Thanks in advance for any help

mid4 = mid1-1;//this is the sum of the previous formula times number of months minus 1
{
if (mid4 == 0)// Checks the mid4 int to deduce whether it is zero or not.
throw std::overflow_error("Divide by zero exception");
}

try {
i = mid4;
} catch (std::overflow_error e) {
cout << e.what() << " -> ";
}
cout << i << endl;
You seem to not understand how exceptions work - you can only catch exceptions which are thrown within a try block.

http://www.cplusplus.com/doc/tutorial/exceptions/
Thank you LB, I read that, and I am still trying to understand what the process is, In my program, if mid4 is 0, I want it to throw and exception. I think part of my issue is placement, does it have to be placed at the moment in the program, before something is divided by mid4? and do I have to create the Try area prior to that? So like

Try {
if (mid4 == 0)// Checks the mid4 int to deduce whether it is zero or not.
throw std::overflow_error("Divide by zero exception");
}
Catch (std::overflow_error e)
{ cout << e.what();}
cout << mid4 << endl;
mid5 = mid2 / mid4

Would that be more Correct placement?
Thank You for all the help, I got it working correctly. Thank You, Thank You...
Well, if you throw inside the try like that, then it's pretty pointless...
Topic archived. No new replies allowed.