-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA15-3-5.cpp
More file actions
56 lines (44 loc) · 1.25 KB
/
A15-3-5.cpp
File metadata and controls
56 lines (44 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
// Rule: A15-3-5
// Source line: 25659
// Original file: A15-3-5.cpp
//% $Id: A15-3-5.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <iostream>
#include <stdexcept>
class Exception : public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
const char* what() const noexcept(true) override
{
return "Exception error message";
}
};
void Fn()
{
try
{
// ...
throw std::runtime_error("Error");
// ...
throw Exception("Error");
}
catch (const std::logic_error& e) // Compliant - caught by const reference
{
// Handle exception
}
catch (std::runtime_error& e) // Compliant - caught by reference
{
std::cout << e.what() << "\n"; // "Error" or "Exception error message"
// will be printed, depending upon the
// actual type of thrown object
throw e; // The exception re-thrown is of its original type
}
catch (std::runtime_error e) // Non-compliant - derived types will be caught as the base type
{
std::cout
<< e.what()
<< "\n"; // Will always call what() method from std::runtime_error
throw e; // The exception re-thrown is of the std::runtime_error type,
// not the original exception type
}
}