-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA15-0-4.cpp
More file actions
119 lines (90 loc) · 2.14 KB
/
A15-0-4.cpp
File metadata and controls
119 lines (90 loc) · 2.14 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Rule: A15-0-4
// Source line: 22508
// Original file: A15-0-4.cpp
//% $Id: A15-0-4.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <stdexcept>
#include <vector>
class InvalidArguments : public std::logic_error // Compliant - invalid
// arguments error is
// "unchecked" exception
{
public:
using std::logic_error::logic_error;
};
class OutOfMemory : public std::bad_alloc // Compliant - insufficient memory
// error is "unchecked" exception
{
public:
using std::bad_alloc::bad_alloc;
};
class DivisionByZero : public std::logic_error
// Compliant - division by zero
// error is "unchecked"
// exception
{
public:
using std::logic_error::logic_error;
};
class CommunicationError : public std::logic_error
// Non-compliant // communication error
// should be "checked"
// exception but defined to be "unchecked"
{
public:
using std::logic_error::logic_error;
};
double Division(std::int32_t a, std::int32_t b) noexcept(false)
{
// ...
if (b == 0)
{
throw DivisionByZero(
"Division by zero error"); // Unchecked exception thrown correctly
}
// ...
}
void Allocate(std::uint32_t bytes) noexcept(false)
{
// ...
throw OutOfMemory(); // Unchecked exception thrown correctly
}
void InitializeSocket() noexcept(false)
{
bool validParameters = true;
// ...
if (!validParameters)
{
throw InvalidArguments("Invalid parameters passed");
// Unchecked
// exception
// thrown
// correctly
}
}
void SendData(std::int32_t socket) noexcept(false)
{
// ...
bool isSentSuccessfully = true;
// ...
if (!isSentSuccessfully)
{
throw CommunicationError("Could not send data");
// Unchecked exception
// thrown when checked
// exception should
// be.
}
}
void IterateOverContainer(const std::vector<std::int32_t>& container,
std::uint64_t length) noexcept(false)
{
for (std::uint64_t idx{0U}; idx < length; ++idx)
{
int32_t value = container.at(idx); // at() throws std::out_of_range
// exception when passed integer
// exceeds the size of container.
// Unchecked exception thrown
// correctly
}
}