forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_example_userexception.cpp
163 lines (139 loc) · 3.04 KB
/
cpp_example_userexception.cpp
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
158
159
160
161
162
163
/**
* @file
*
* @brief This example explains how to define user defined exception.
*
* That means that the user can implement their own exceptions,
* e.g. potentially derived from their own base class with its
* own what() information.
* The user could even decide to use another mechanism
* instead of what().
*
* The only requirements are: They must be named exactly like
* the original classes and KDBException constructor must take a key
* as argument (which has all information like error and warnings).
*
* It does not matter from which class the exceptions are derived
* or which members they have (if they are binary compatible) as
* long as they are used everywhere.
*
* Never use non-binary compatible user exceptions if you do not
* use them everywhere where you include any of elektras header
* files!
*
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "kdbmacros.h"
#include <stdexcept>
class UserException : public std::exception
{
public:
virtual const char * what () const throw () override
{
return "User Exception";
}
};
namespace kdb
{
class Exception : public UserException
{
public:
virtual const char * what () const throw () override
{
return "User Exception: Exception thrown by Elektra";
}
};
class KeyException : public Exception
{
public:
virtual const char * what () const throw () override
{
return "User Exception: Exception thrown by a Key";
}
};
class KeyNotFoundException : public Exception
{
public:
explicit KeyNotFoundException (std::string const &)
{
}
virtual const char * what () const throw ()
{
return "User Exception: Key not found";
}
private:
};
class KeyTypeMismatch : public KeyException
{
public:
virtual const char * what () const throw () override
{
return "User Exception: Binary or String key mismatch";
}
};
class KeyInvalidName : public KeyException
{
public:
KeyInvalidName (const std::string & name ELEKTRA_UNUSED, const std::string & more ELEKTRA_UNUSED)
{
}
virtual const char * what () const throw () override
{
return "User Exception: Invalid Keyname";
}
};
class KeyTypeConversion : public KeyException
{
public:
virtual const char * what () const throw () override
{
return "User Exception: Exception thrown by get/set";
}
};
} // namespace kdb
#define USER_DEFINED_EXCEPTIONS
#include <key.hpp>
namespace kdb
{
class KDBException : public Exception
{
public:
explicit KDBException (Key key) : m_key (key)
{
}
virtual ~KDBException () throw ()
{
}
virtual const char * what () const throw () override
{
return "User Exception: KDB";
}
protected:
Key m_key;
};
class ContractException : public KDBException
{
public:
explicit ContractException (Key key) : KDBException (key)
{
}
~ContractException () noexcept override = default;
const char * what () const noexcept override
{
if (!m_key)
{
return "Malformed contract";
}
return KDBException::what ();
}
};
} // namespace kdb
#include <kdb.hpp>
int main ()
{
kdb::Key k ("abc", KEY_END);
kdb::KDB kdb;
kdb::KeySet ks;
kdb.get (ks, k);
}