44 lines
780 B
C++
44 lines
780 B
C++
#include <iostream>
|
|
#include <algorithm>
|
|
#include <vector>
|
|
#include <stdexcept>
|
|
|
|
using namespace std;
|
|
|
|
class ExpectFail {
|
|
public:
|
|
string message;
|
|
|
|
ExpectFail(string m) : message(m) {};
|
|
};
|
|
|
|
void expect(bool test, string msg) {
|
|
if(!test) {
|
|
throw ExpectFail("Expectation failed.");
|
|
}
|
|
}
|
|
|
|
int area(int length, int width) {
|
|
expect(length > 0 && width > 0, "Bad area given.");
|
|
|
|
return length * width;
|
|
}
|
|
|
|
int main() {
|
|
vector<int> numbers;
|
|
|
|
try {
|
|
numbers.push_back(100);
|
|
numbers.push_back(-100);
|
|
|
|
cout << "Area " << area(numbers.at(1), 100) << "\n";
|
|
|
|
return 0;
|
|
} catch(out_of_range &e) {
|
|
cout << "Out of range ERROR: " << e.what() << "\n";
|
|
return 2;
|
|
} catch(...) {
|
|
cout << "Exception: something went wrong\n";
|
|
return 3;
|
|
}
|
|
}
|