-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA15-1-1.cpp
More file actions
88 lines (69 loc) · 1.42 KB
/
A15-1-1.cpp
File metadata and controls
88 lines (69 loc) · 1.42 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
// Rule: A15-1-1
// Source line: 23262
// Original file: A15-1-1.cpp
//% $Id: A15-1-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <memory>
#include <stdexcept>
class A
{
// Implementation
};
class MyException : public std::logic_error
{
public:
using std::logic_error::logic_error;
// Implementation
};
void F1()
{
throw -1; // Non-compliant - integer literal thrown
}
void F2()
{
throw nullptr; // Non-compliant - null-pointer-constant thrown
}
void F3()
{
throw A(); // Non-compliant - user-defined type that does not inherit from
// std::exception thrown
}
void F4()
{
throw std::logic_error{
"Logic Error"}; // Compliant - std library exception thrown
}
void F5()
{
throw MyException{"Logic Error"};
// Compliant - user-defined type that
// inherits from std::exception thrown
}
void F6()
{
throw std::make_shared<std::exception>(
std::logic_error("Logic Error")); // Non-compliant - shared_ptr does
// not inherit from std::exception
}
void F7()
{
try
{
F6();
}
catch (std::exception& e)
{
// Handle an exception
}
catch (std::shared_ptr<std::exception>& e)
// std::shared_ptr<std::exception>
// type will be caught here, but
// unable to access
// std::logic_error information
{
// Handle an exception
}
// An exception of
// std::shared_ptr<std::exception> type will not
// be caught here
// An exception of
}