forked from halterman/CppBook-SourceCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrethrow.cpp
More file actions
34 lines (30 loc) · 896 Bytes
/
rethrow.cpp
File metadata and controls
34 lines (30 loc) · 896 Bytes
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
#include <iostream>
#include <fstream>
#include <vector>
void filter(std::vector<int>& v, int i) {
v.at(i)++;
}
void compute(std::vector<int>& a) {
for (int i = 0; i < 6; i++) {
try {
filter(a, i);
}
catch (std::exception& ex) {
std::cout << "********************************\n";
std::cout << "* For loop terminated prematurely\n";
std::cout << "* when i = " << i << '\n';
std::cout << "********************************\n";
throw ex; // Rethrow the same exception
}
}
}
int main() {
std::vector<int> list { 10, 20, 30, 40, 50 };
try {
compute(list);
}
catch (std::exception& e) {
std::cout << "Caught an exception: " << e.what() << '\n';
}
std::cout << "Program finished\n";
}