#include <iostream> #include <stdexcept> using namespace std; int GetWeight() { int weightParam; // User defined weight // Get user data cout << "Enter weight (in pounds): "; cin >> weightParam; // Error checking, non-negative weight if (weightParam < 0) { throw runtime_error("Invalid weight."); } return weightParam; } int GetHeight() { int heightParam; // User defined height // Get user data cout << "Enter height (in inches): "; cin >> heightParam; // Error checking, non-negative height if (heightParam < 0) { throw runtime_error("Invalid height."); } return heightParam; } int main() { int weightVal; // User defined weight (lbs) int heightVal; // User defined height (in) float bmiCalc; // Resulting BMI char quitCmd; // Indicates quit/continue quitCmd = 'a'; while (quitCmd != 'q') { try { // Get user data weightVal = GetWeight(); heightVal = GetHeight(); // Calculate BMI and print user health info if no input error // Source: http://www.cdc.gov/ bmiCalc = (static_cast<float>(weightVal) / static_cast<float>(heightVal * heightVal)) * 703.0; cout << "BMI: " << bmiCalc << endl; cout << "(CDC: 18.6-24.9 normal)" << endl; } catch (runtime_error &excpt) { // Prints the error message passed by throw statement cout << excpt.what() << endl; cout << "Cannot compute health info." << endl; } // Prompt user to continue/quit cout << endl << "Enter any key ('q' to quit): "; cin >> quitCmd; } return 0; }
Suppose getWeight() throws an exception of type Exception. GetWeight() immediately exits, up to main() where the call was in a try block, so the catch block catches the exception.
Note the clarity of the code in main(). Without exceptions, GetWeight() would have had to somehow indicate failure, perhaps returning -1. Then main() would have needed an if-else statement to detect such failure, obscuring the normal code.
If no handler is found going up the call hierarchy, then terminate() is called, which typically aborts the program.