Branch data Line data Source code
1 : : // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #ifndef __EXCEPTIONS_H
16 : : #define __EXCEPTIONS_H 1
17 : :
18 : : #include <stdexcept>
19 : : #include <string>
20 : : #include <sstream>
21 : :
22 : : namespace isc {
23 : :
24 : : ///
25 : : /// This is a base class for exceptions thrown from the DNS library module.
26 : : /// Normally, the exceptions are thrown via a convenient shortcut macro,
27 : : /// @ref isc_throw, which automatically gives trivial parameters for the
28 : : /// exception such as the file name and line number where the exception is
29 : : /// triggered.
30 : : ///
31 [ - + ]: 1 : class Exception : public std::exception {
32 : : public:
33 : : ///
34 : : /// \name Constructors and Destructor
35 : : ///
36 : : //@{
37 : : /// \brief Constructor for a given type for exceptions with file name and
38 : : /// file line number.
39 : : ///
40 : : /// @param file the file name where the exception was thrown.
41 : : /// @param line the line in \a file where the exception was thrown.
42 : : /// @param what a description (type) of the exception.
43 : 1219 : Exception(const char* file, size_t line, const char* what) :
44 [ + + ][ - + ]: 2228 : file_(file), line_(line), what_(what) {}
[ - + ][ + ]
[ - + ][ # ]
45 : :
46 : : /// \brief Constructor for a given type for exceptions with file name and
47 : : /// file line number.
48 : : ///
49 : : /// @param file the file name where the exception was thrown.
50 : : /// @param line the line in \a file where the exception was thrown.
51 : : /// @param what a description (type) of the exception.
52 : : Exception(const char* file, size_t line, const std::string& what) :
53 [ - + ]: 9 : file_(file), line_(line), what_(what) {}
54 : :
55 : : /// The destructor
56 : 2246 : virtual ~Exception() throw() {}
57 : : //@}
58 : : private:
59 : : ///
60 : : /// The assignment operator is intentionally disabled.
61 : : ///
62 : : void operator=(const Exception& src);
63 : :
64 : : public:
65 : : ///
66 : : /// \name Methods Reimplemented against the Standard Exception Class
67 : : ///
68 : : //@{
69 : : /// \brief Returns a C-style character string of the cause of the exception.
70 : : ///
71 : : /// Note: we normally don't use exception specifications, but this is an
72 : : /// "exception" to that policy as it's enforced by the base class.
73 : : ///
74 : : /// @return A C-style character string of the exception cause.
75 : : virtual const char* what() const throw();
76 : : //@}
77 : :
78 : : ///
79 : : /// \name Getter Methods
80 : : ///
81 : : //@{
82 : : /// \brief Gets a string describing the cause of the exception.
83 : : ///
84 : : /// @return the cause string.
85 : : const std::string& getMessage() const { return (what_); }
86 : :
87 : : /// \brief Gets the file name where the exception was thrown.
88 : : ///
89 : : /// @return a C-style string of the file name.
90 : 0 : const char* getFile() const { return (file_); }
91 : :
92 : : /// \brief Gets the line number of the file where the exception was thrown.
93 : : ///
94 : : /// @return an integer specifying the line number.
95 : 0 : size_t getLine() const { return (line_); }
96 : : //@}
97 : :
98 : : private:
99 : : const char* const file_;
100 : : size_t line_;
101 : : const std::string what_;
102 : : };
103 : :
104 : : /// \brief A generic exception that is thrown if a parameter given
105 : : /// to a method would refer to or modify out-of-range data.
106 : 56 : class OutOfRange : public Exception {
107 : : public:
108 : 10 : OutOfRange(const char* file, size_t line, const char* what) :
109 [ + - ][ + - ]: 81 : isc::Exception(file, line, what) {}
110 : : };
111 : :
112 : : /// \brief A generic exception that is thrown if a parameter given
113 : : /// to a method or function is considered invalid and no other specific
114 : : /// exceptions are suitable to describe the error.
115 : 167 : class InvalidParameter : public Exception {
116 : : public:
117 : 44 : InvalidParameter(const char* file, size_t line, const char* what) :
118 [ + - ][ + - ]: 170 : isc::Exception(file, line, what) {}
[ + + ][ + - ]
[ + - ]
119 : : };
120 : :
121 : : /// \brief A generic exception that is thrown if a parameter given
122 : : /// to a method is considered invalid in that context.
123 [ # # ]: 689 : class BadValue : public Exception {
124 : : public:
125 : 521 : BadValue(const char* file, size_t line, const char* what) :
126 [ + - ][ + - ]: 689 : isc::Exception(file, line, what) {}
[ + - ][ + - ]
[ + + ]
127 : : };
128 : :
129 : : /// \brief A generic exception that is thrown if a function is called
130 : : /// in a prohibited way.
131 : : ///
132 : : /// For example, this can happen if a class method is called when the object's
133 : : /// state does not allow that particular method.
134 : 6 : class InvalidOperation : public Exception {
135 : : public:
136 : : InvalidOperation(const char* file, size_t line, const char* what) :
137 [ + - ][ # # ]: 6 : isc::Exception(file, line, what) {}
[ + - ]
138 : : };
139 : :
140 : : ///
141 : : /// \brief A generic exception that is thrown when an unexpected
142 : : /// error condition occurs.
143 : : ///
144 : 13 : class Unexpected : public Exception {
145 : : public:
146 : 0 : Unexpected(const char* file, size_t line, const char* what) :
147 [ + - ][ + - ]: 13 : isc::Exception(file, line, what) {}
[ + - ][ # # ]
[ + - ][ # # ]
[ # # ]
148 : : };
149 : :
150 : : ///
151 : : /// \brief A generic exception that is thrown when a function is
152 : : /// not implemented.
153 : : ///
154 : : /// This may be due to unfinished implementation or in case the
155 : : /// function isn't even planned to be provided for that situation.
156 : 22 : class NotImplemented : public Exception {
157 : : public:
158 : 17 : NotImplemented(const char* file, size_t line, const char* what) :
159 [ + - ][ # # ]: 22 : isc::Exception(file, line, what) {}
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
160 : : };
161 : :
162 : : ///
163 : : /// A shortcut macro to insert known values into exception arguments.
164 : : ///
165 : : /// It allows the \c stream argument to be part of a statement using an
166 : : /// \c ostream object and its \c operator<<. For example,
167 : : /// \code int x = 10;
168 : : /// isc_throw(SomeException, "Error happened, parameter: " << x);
169 : : /// \endcode
170 : : /// will throw an exception of class \c SomeException whose \c what string
171 : : /// will be <code>"Error happened, parameter: 10"</code>.
172 : : ///
173 : : /// Note: the stream related operations or creation of the exception object
174 : : /// may itself throw an exception (specifically \c std::bad_alloc).
175 : : /// Even though it should be very rare, we may have to address this issue later.
176 : : ///
177 : : /// Note: in general we hate macros and avoid using it in the code. This is
178 : : /// one of few exceptions to that policy. inline functions cannot be used
179 : : /// for embedding \c __FILE__ and \c __LINE__. This is the main reason why
180 : : /// this is defined as a macro. The convenience for the ostream is a secondary
181 : : /// purpose (if that were the only possible reason we should rather avoid
182 : : /// using a macro).
183 : : #define isc_throw(type, stream) \
184 : : do { \
185 : : std::ostringstream oss__; \
186 : : oss__ << stream; \
187 : : throw type(__FILE__, __LINE__, oss__.str().c_str()); \
188 : : } while (1)
189 : :
190 : : ///
191 : : /// Similar as isc_throw, but allows the exception to have one additional
192 : : /// parameter (the stream/text goes first)
193 : : #define isc_throw_1(type, stream, param1) \
194 : : do { \
195 : : std::ostringstream oss__; \
196 : : oss__ << stream; \
197 : : throw type(__FILE__, __LINE__, oss__.str().c_str(), param1); \
198 : : } while (1)
199 : :
200 : : ///
201 : : /// Similar as isc_throw, but allows the exception to have two additional
202 : : /// parameters (the stream/text goes first)
203 : : #define isc_throw_2(type, stream, param1, param2) \
204 : : do { \
205 : : std::ostringstream oss__; \
206 : : oss__ << stream; \
207 : : throw type(__FILE__, __LINE__, oss__.str().c_str(), param1, param2); \
208 : : } while (1)
209 : :
210 : : ///
211 : : /// Similar as isc_throw, but allows the exception to have three additional
212 : : /// parameters (the stream/text goes first)
213 : : #define isc_throw_3(type, stream, param1, param2, param3) \
214 : : do { \
215 : : std::ostringstream oss__; \
216 : : oss__ << stream; \
217 : : throw type(__FILE__, __LINE__, oss__.str().c_str(), param1, param2,\
218 : : param3); \
219 : : } while (1)
220 : :
221 : : ///
222 : : /// Similar as isc_throw, but allows the exception to have four additional
223 : : /// parameters (the stream/text goes first)
224 : : #define isc_throw_4(type, stream, param1, param2, param3, param4) \
225 : : do { \
226 : : std::ostringstream oss__; \
227 : : oss__ << stream; \
228 : : throw type(__FILE__, __LINE__, oss__.str().c_str(), param1, param2,\
229 : : param3, param4); \
230 : : } while (1)
231 : :
232 : : }
233 : : #endif // __EXCEPTIONS_H
234 : :
235 : : // Local Variables:
236 : : // mode: c++
237 : : // End:
|