-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.cc
More file actions
70 lines (62 loc) · 1.25 KB
/
exceptions.cc
File metadata and controls
70 lines (62 loc) · 1.25 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
69
70
#include <iostream>
#include <stdlib.h>
#include <time.h>
class CurveBall
{
public:
CurveBall(std::string message = "CurveBall")
: message(message)
{}
std::string
what() const
{
return message;
}
private:
std::string message;
};
class TooManyExceptions
{
public:
TooManyExceptions(std::string message = "Too Many Exceptions")
: message(message)
{}
std::string
what() const
{
return message;
}
private:
std::string message;
};
void
genException()
{
auto num = rand() % 1000;
if (num < 250) {
throw CurveBall{"Here's a CurveBall"};
}
}
int
main ()
{
srand(time(NULL));
int count = 0;
try {
for (int i {}; i < 1000; i++)
{
try {
genException();
} catch (const CurveBall& ex) {
count++;
std::cout << ex.what() << std::endl;
if (count > 10) {
throw TooManyExceptions();
}
}
}
} catch (const TooManyExceptions &ex) {
std::cout << ex.what() << std::endl;
}
std::cout << "CurveBall thrown " << count << " times" << std::endl;
}