Does the following (using your working) example crash your program? It should. If it doesn't you have a non standard compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <stdexcept>
void BasePlusCommissionEmployee(double salary)
{
if (salary >= 0.0)
double baseSalary = salary;
else
throw std::invalid_argument( "Salary must be greater than 0.0" );
}
int main()
{
BasePlusCommissionEmployee(-1);
}
| |
Edit: To further clarify: Every thrown exception
needs to be caught. If you don't catch it, the program
will crash. When an exception is thrown in a manner such as the 'working' example you presented, it unwinds the call stack until it finds a catch statement that can handle it's exception. Since there was nothing to catch it in the function, it returns to wherever called the function, and tries to find something to catch it there. Again, if it doesn't find anything, it returns again, all the way up until it eventually reaches
main()
, at which point if main doesn't catch it, it goes up the call stack again, and at this point the program crashes. The program does not resume after the throw statement, it resumes after the catch statement.