-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA15-1-3.cpp
More file actions
116 lines (86 loc) · 1.62 KB
/
A15-1-3.cpp
File metadata and controls
116 lines (86 loc) · 1.62 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
// Rule: A15-1-3
// Source line: 23597
// Original file: A15-1-3.cpp
//% $Id: A15-1-3.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
static std::string ComposeMessage(const char* file,
const char* func,
std::int32_t line,
const std::string& message) noexcept
{
std::stringstream s;
s << "(" << file << ", " << func << ":" << line << "): " << message;
return s.str();
}
void F1()
{
// ...
throw std::logic_error("Error");
}
void F2()
{
// ...
throw std::logic_error("Error"); // Non-compliant - both exception type and
// error message are not unique
}
void F3()
{
// ...
throw std::invalid_argument(
"Error"); // Compliant - exception type is unique
}
void F4() noexcept(false)
{
// ...
throw std::logic_error("f3(): preconditions were not met"); // Compliant // error
// message is
// unique
}
void F5() noexcept(false)
{
// ...
throw std::logic_error(ComposeMessage(
__FILE__,
__func__,
__LINE__,
"postconditions were not met"));
// Compliant - error message is unique
}
void F6() noexcept
{
try
{
F1();
F2();
F3();
}
catch (std::invalid_argument& e)
{
std::cout << e.what() << ’\n’;
// Only f3() throws this type of
// exception, it is easy to deduce which
// function threw
}
catch (std::logic_error& e)
{
std::cout << e.what() << ’\n’;
// f1() and f2() throw exactly the same
// exceptions, unable to deduce which
// function threw
}
try
{
F4();
F5();
}
catch (std::logic_error& e)
{
std::cout << e.what() << ’\n’;
}
// Debugging process simplified, because
// of unique error message it is known
// which function threw
}