class InsufficientFundsException : public runtime_error {
double shortfall;
public:
InsufficientFundsException(double s)
: runtime_error("Insufficient funds"), shortfall(s) {}
double getShortfall() const { return shortfall; }
};
void withdraw(double& balance, double amount) {
if (amount > balance)
throw InsufficientFundsException(amount - balance);
balance -= amount;
}
try {
double bal = 50.0;
withdraw(bal, 100.0);
} catch (const InsufficientFundsException& e) {
cout << e.what() << " Short by: " << e.getShortfall();
}
std::exception
├── std::runtime_error
└── std::logic_error
├── std::invalid_argument
└── std::out_of_range
Create ValidationError extending invalid_argument with a getField() method.
Write parseAge(int n) that throws ValidationError("age") if n < 0 or n > 150.
Click "Run" to execute your code.