-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathexceptions.cpp
More file actions
68 lines (56 loc) · 1.52 KB
/
Copy pathexceptions.cpp
File metadata and controls
68 lines (56 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<vector>
int quotient(int top, int bot) {
if (bot == 0) {
throw 0;
}
return top / bot;
}
int mult(int a, int b) {
return a * b;
}
void doStuff(int x, int y) {
try {
if ( (x == 13) || (y == 13) ) {
throw 13;
}
std::cout << " " << x << "*" << y << "=" << mult(x,y) << std::endl;
std::cout << " " << x << "/" << y << "=" << quotient(x,y) << std::endl;
}
catch(int e) {
if (e==0) {
std::cout << std::endl << "Warning: attempt to divide by 0!" << std::endl;
}
if (e==13) {
std::cout << std::endl << "Warning: attempt to use unlucky number!" << std::endl;
}
}
}
int main() {
std::vector<int> vec = {1, 2, 3, 4};
std::cout << "doStuff(2,3):" << std::endl;
doStuff(2,3);
std::cout << std::endl;
std::cout << "doStuff(2,0):" << std::endl;
doStuff(2,0);
std::cout << std::endl;
std::cout << "doStuff(39,13):" << std::endl;
doStuff(39,13);
std::cout << std::endl;
std::cout << "Vector: " << std::endl;
for (auto & ii : vec) {
std::cout << ii << " ";
}
std::cout << std::endl;
std::cout << "First 10 elements of vec: " << std::endl;
try {
for (int ii=0;ii<10;ii++) {
std::cout << vec.at(ii) << " ";
}
}
catch (const std::out_of_range &) {
std::cout << "You tried to access something I didn't have!" << std::endl;
}
std::cout << std::endl;
return 0;
}