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
#include <iostream>
using namespace std;
void throw_exception(int i) {
cout<< " Throwing exception "<< i << endl;
throw i;
}
void do_catch(int i) {
try {
throw i;
}
catch(int error) {
cout<<"Exception " << i << "successfully catched."<<endl;
}
}
void maybe_catch(int i) {
if(i<42) {
try {
throw_exception(i);
}
catch(int errror) {
// Yay, catched.
cout<<"Exception " << i << " successfully catched."<<endl;
}
}
else {
// We don't catch anything here.
throw_exception(i);
}
}
int main() {
cout<<"Enter a number." <<endl;
int i;
cin >> i;
maybe_catch(i);
do_catch(i);
// For good measure, just throw an exception here.
throw (i+20);
}