cpp
1
2double hmean(double a, double b) {
3
4 if (a == -b )
5
6 throw runtime_error("Runtime error occurred.");
7
8 return 2.0*a*b/(a + b);
9
10}
11
12
13
14int main() {
15
16 double x = 10;
17
18 double y = -10;
19
20 try {
21
22 int result = hmean(x, y);
23
24 cout << "hmean: " << result << endl;
25
26 }
27
28 catch (const runtime_error& e) {
29
30 cout << "Caught: " << e.what() << endl;
31
32 }
33
34 catch (...) {
35
36 cout << "Caught an unknown exception." << endl;
37
38 }
39
40 return 0;
41
42}
43