forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49-exception_scope.cpp
62 lines (59 loc) · 1.47 KB
/
49-exception_scope.cpp
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
#include <iostream>
#include <stdexcept>
int F(int start, int end)
{
int total{0};
try
{
if (start < 1)
throw std::runtime_error{"Start value too small"};
for (int i{start}; i <= end; i++)
{
try
{
if (i > 5)
throw std::domain_error{"Too big"};
total += i;
}
catch (std::domain_error &e)
{
std::cout << "F: Caught domain_error" << std::endl;
//Question: What happens if the following line is uncommented?
//throw std::domain_error("Uh oh");
}
}
}
catch (std::runtime_error &e)
{
std::cout << "F: Caught runtime_error" << std::endl;
return 1000;
}
return total;
}
int main()
{
std::cout << "main (1)" << std::endl;
int sum{1000};
try
{
sum = F(2, 4);
std::cout << "main (2): sum = " << sum << std::endl;
sum = F(3, 6);
std::cout << "main (3): sum = " << sum << std::endl;
}
catch (std::domain_error &e)
{
std::cout << "main: Caught domain_error" << std::endl;
}
catch (std::runtime_error &e)
{
std::cout << "main: Caught runtime_error" << std::endl;
}
catch (...)
{
std::cout << "main: Caught something?" << std::endl;
}
std::cout << "main (4)" << std::endl;
std::cout << "sum = " << sum << std::endl;
return 0;
}