Study results from Stroustrup's PPP3 book.
This commit is contained in:
parent
6363457d0f
commit
7a1093f982
10 changed files with 579 additions and 1 deletions
44
PPP3/ex05.cpp
Normal file
44
PPP3/ex05.cpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue