-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA15-3-2.cpp
More file actions
157 lines (109 loc) · 2.36 KB
/
A15-3-2.cpp
File metadata and controls
157 lines (109 loc) · 2.36 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Rule: A15-3-2
// Source line: 24744
// Original file: A15-3-2.cpp
//% $Id: A15-3-2.cpp 309502 2018-02-28 09:17:39Z michal.szczepankiewicz $
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <memory>
/// @checkedException
class CommunicationError : public std::exception
{
// Implementation
};
/// @throw CommunicationError Exceptional communication errors
extern void Send(std::uint8_t* buffer) noexcept(false);
void SendData1(std::uint8_t* data) noexcept(false)
{
try
{
Send(data);
}
catch (CommunicationError& e)
{
std::cerr << "Communication error occured" << std::endl;
throw; // Non-compliant - exception is not handled, just re-thrown
}
}
extern void BusRestart() noexcept;
extern void BufferClean() noexcept;
void SendData2(std::uint8_t* data) noexcept(false)
{
try
{
Send(data);
}
catch (CommunicationError& e)
{
std::cerr << "Communication error occured" << std::endl;
BufferClean();
throw; // Compliant - exception is partially handled and re-thrown
}
}
void F1() noexcept
{
std::uint8_t* buffer = nullptr;
// ...
try
{
SendData2(buffer);
}
catch (CommunicationError& e)
{
std::cerr << "Communication error occured" << std::endl;
BusRestart();
// Compliant - including SendData2() exception handler, exception is now
// fully handled
}
}
void SendData3(std::uint8_t* data) noexcept
{
try
{
Send(data);
}
catch (CommunicationError& e)
{
std::cerr << "Communication error occured" << std::endl;
BufferClean();
BusRestart();
// Compliant - exception is fully handled
}
}
struct A
{
std::uint32_t x;
};
std::unique_ptr<A[]> Func1()
{
//rather throws std::bad_alloc
return std::make_unique<A[]>(999999999999999999);
}
std::unique_ptr<A[]> Func2()
{
//does not catch std::bad_alloc
//because nothing meaningful can be done here
return Func1();
}
std::unique_ptr<A[]> Func3()
{
//does not catch std::bad_alloc
//because nothing meaningful can be done here
return Func2();
}
extern void Cleanup() noexcept;
int main(void)
{
try
{
Func3();
}
catch (const std::exception& ex)
{
//catches std::bad_alloc here and
//terminates the application
//gracefully
Cleanup();
}
return 0;
}