MySQL++  3.3.0
exceptions.h
Go to the documentation of this file.
1 
7 /***********************************************************************
8  Copyright © 1998 by Kevin Atkinson, © 1999-2001 by MySQL AB, and
9  © 2004-2010, 2018 by Educational Technology Resources, Inc. Others may
10  also hold copyrights on code in this file. See the CREDITS.txt file
11  in the top directory of the distribution for details.
12 
13  This file is part of MySQL++.
14 
15  MySQL++ is free software; you can redistribute it and/or modify it
16  under the terms of the GNU Lesser General Public License as published
17  by the Free Software Foundation; either version 2.1 of the License, or
18  (at your option) any later version.
19 
20  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
23  License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with MySQL++; if not, write to the Free Software
27  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
28  USA
29 ***********************************************************************/
30 
31 #if !defined(MYSQLPP_EXCEPTIONS_H)
32 #define MYSQLPP_EXCEPTIONS_H
33 
34 #include "options.h"
35 
36 #include <exception>
37 #include <string>
38 #include <sstream>
39 #include <typeinfo>
40 
41 namespace mysqlpp {
42 
44 
45 class MYSQLPP_EXPORT Exception : public std::exception
46 {
47 public:
49  Exception(const Exception& e) MAY_THROW() :
50  std::exception(e),
51  what_(e.what_)
52  {
53  }
54 
56  Exception& operator=(const Exception& rhs) throw()
57  {
58  what_ = rhs.what_;
59  return *this;
60  }
61 
63  ~Exception() throw() { }
64 
66  virtual const char* what() const throw()
67  {
68  return what_.c_str();
69  }
70 
71 protected:
73  Exception(const char* w = "") throw() :
74  what_(w)
75  {
76  }
77 
79  Exception(const std::string& w) throw() :
80  what_(w)
81  {
82  }
83 
85  std::string what_;
86 };
87 
88 
90 
91 class MYSQLPP_EXPORT BadConversion : public Exception
92 {
93 public:
94  const char* type_name;
95  std::string data;
96  size_t retrieved;
97  size_t actual_size;
98 
106  BadConversion(const char* tn, const char* d,
107  size_t r, size_t a) :
108  Exception("Bad type conversion: \""),
109  type_name(tn),
110  data(d),
111  retrieved(r),
112  actual_size(a)
113  {
114  what_ += d ? d : "<NULL>";
115  what_ += "\" incompatible with \"";
116  what_ += tn;
117  what_ += "\" type";
118  }
119 
127  BadConversion(const std::string& w, const char* tn,
128  const char* d, size_t r, size_t a) :
129  Exception(w),
130  type_name(tn),
131  data(d),
132  retrieved(r),
133  actual_size(a)
134  {
135  }
136 
142  explicit BadConversion(const char* w = "") :
143  Exception(w),
144  type_name("unknown"),
145  data(""),
146  retrieved(0),
147  actual_size(0)
148  {
149  }
150 
152  ~BadConversion() throw() { }
153 };
154 
155 
160 
161 class MYSQLPP_EXPORT BadFieldName : public Exception
162 {
163 public:
167  explicit BadFieldName(const char* bad_field) :
168  Exception(std::string("Unknown field name: ") + bad_field)
169  {
170  }
171 
173  ~BadFieldName() throw() { }
174 };
175 
176 
179 
180 class MYSQLPP_EXPORT BadIndex : public Exception
181 {
182 public:
188  explicit BadIndex(const char* what, int bad_index, int max_index) :
189  Exception()
190  {
191  std::ostringstream outs;
192  outs << "Index " << bad_index << " on " << what <<
193  " out of range, max legal index is " << max_index;
194  what_ = outs.str();
195  }
196 
198  ~BadIndex() throw() { }
199 };
200 
201 
204 
205 class MYSQLPP_EXPORT BadOption : public Exception
206 {
207 public:
209  explicit BadOption(const char* w, const std::type_info& ti) :
210  Exception(w),
211  ti_(ti)
212  {
213  }
214 
216  explicit BadOption(const std::string& w, const std::type_info& ti) :
217  Exception(w),
218  ti_(ti)
219  {
220  }
221 
226  const std::type_info& what_option() const { return ti_; }
227 
228 private:
229  const std::type_info& ti_;
230 };
231 
232 
237 
238 class MYSQLPP_EXPORT BadParamCount : public Exception
239 {
240 public:
242  explicit BadParamCount(const char* w = "") :
243  Exception(w)
244  {
245  }
246 
248  ~BadParamCount() throw() { }
249 };
250 
253 
254 class MYSQLPP_EXPORT UseQueryError : public Exception
255 {
256 public:
258  explicit UseQueryError(const char* w = "") :
259  Exception(w)
260  {
261  }
262 };
263 
264 
284 
285 class MYSQLPP_EXPORT BadQuery : public Exception
286 {
287 public:
292  explicit BadQuery(const char* w = "", int e = 0) :
293  Exception(w),
294  errnum_(e)
295  {
296  }
297 
302  explicit BadQuery(const std::string& w, int e = 0) :
303  Exception(w),
304  errnum_(e)
305  {
306  }
307 
314  int errnum() const { return errnum_; }
315 
316 private:
317  int errnum_;
318 };
319 
320 
327 
328 class MYSQLPP_EXPORT ConnectionFailed : public Exception
329 {
330 public:
335  explicit ConnectionFailed(const char* w = "", int e = 0) :
336  Exception(w),
337  errnum_(e)
338  {
339  }
340 
349  int errnum() const { return errnum_; }
350 
351 private:
352  int errnum_;
353 };
354 
355 
358 
359 class MYSQLPP_EXPORT DBSelectionFailed : public Exception
360 {
361 public:
366  explicit DBSelectionFailed(const char* w = "", int e = 0) :
367  Exception(w),
368  errnum_(e)
369  {
370  }
371 
380  int errnum() const { return errnum_; }
381 
382 private:
383  int errnum_;
384 };
385 
386 
388 
389 class MYSQLPP_EXPORT MutexFailed : public Exception
390 {
391 public:
393  explicit MutexFailed(const char* w = "lock failed") :
394  Exception(w)
395  {
396  }
397 };
398 
399 
402 
403 class MYSQLPP_EXPORT ObjectNotInitialized : public Exception
404 {
405 public:
407  explicit ObjectNotInitialized(const char* w = "") :
408  Exception(w)
409  {
410  }
411 };
412 
413 
415 
416 class MYSQLPP_EXPORT SelfTestFailed : public Exception
417 {
418 public:
420  explicit SelfTestFailed(const std::string& w) :
421  Exception(w)
422  {
423  }
424 };
425 
426 
437 
438 class MYSQLPP_EXPORT TypeLookupFailed : public Exception
439 {
440 public:
442  explicit TypeLookupFailed(const std::string& w) :
443  Exception(w)
444  {
445  }
446 };
447 
448 
455 
456 class MYSQLPP_EXPORT BadInsertPolicy : public Exception
457 {
458 public:
460  explicit BadInsertPolicy(const std::string& w) :
461  Exception(w)
462  {
463  }
464 };
465 
466 
467 } // end namespace mysqlpp
468 
469 #endif // !defined(MYSQLPP_EXCEPTIONS_H)
Exception thrown when a bad type conversion is attempted.
Definition: exceptions.h:92
BadConversion(const char *w="")
Create exception object, with error string only.
Definition: exceptions.h:142
std::string data
string form of data we tried to convert
Definition: exceptions.h:95
size_t actual_size
documentation needed!
Definition: exceptions.h:97
~BadConversion()
Destroy exception.
Definition: exceptions.h:152
BadConversion(const char *tn, const char *d, size_t r, size_t a)
Create exception object, building error string dynamically.
Definition: exceptions.h:106
const char * type_name
name of type we tried to convert to
Definition: exceptions.h:94
BadConversion(const std::string &w, const char *tn, const char *d, size_t r, size_t a)
Create exception object, given completed error string.
Definition: exceptions.h:127
size_t retrieved
documentation needed!
Definition: exceptions.h:96
Exception thrown when a requested named field doesn't exist.
Definition: exceptions.h:162
~BadFieldName()
Destroy exception.
Definition: exceptions.h:173
BadFieldName(const char *bad_field)
Create exception object.
Definition: exceptions.h:167
Exception thrown when an object with operator [] or an at() method gets called with a bad index.
Definition: exceptions.h:181
BadIndex(const char *what, int bad_index, int max_index)
Create exception object.
Definition: exceptions.h:188
~BadIndex()
Destroy exception.
Definition: exceptions.h:198
Exception thrown when an insert policy is too strict to create a valid INSERT statement.
Definition: exceptions.h:457
BadInsertPolicy(const std::string &w)
Create exception object.
Definition: exceptions.h:460
Exception thrown when you pass an unrecognized option to Connection::set_option().
Definition: exceptions.h:206
BadOption(const std::string &w, const std::type_info &ti)
Create exception object, taking C++ string.
Definition: exceptions.h:216
const std::type_info & what_option() const
Return type information about the option that failed.
Definition: exceptions.h:226
BadOption(const char *w, const std::type_info &ti)
Create exception object, taking C string.
Definition: exceptions.h:209
Exception thrown when not enough query parameters are provided.
Definition: exceptions.h:239
~BadParamCount()
Destroy exception.
Definition: exceptions.h:248
BadParamCount(const char *w="")
Create exception object.
Definition: exceptions.h:242
Exception thrown when the database server encounters a problem while processing your query.
Definition: exceptions.h:286
BadQuery(const std::string &w, int e=0)
Create exception object.
Definition: exceptions.h:302
BadQuery(const char *w="", int e=0)
Create exception object.
Definition: exceptions.h:292
int errnum() const
Return the error number corresponding to the error message returned by what()
Definition: exceptions.h:314
Exception thrown when there is a problem related to the database server connection.
Definition: exceptions.h:329
int errnum() const
Return the error number corresponding to the error message returned by what(), if any.
Definition: exceptions.h:349
ConnectionFailed(const char *w="", int e=0)
Create exception object.
Definition: exceptions.h:335
Exception thrown when the program tries to select a new database and the database server refuses for ...
Definition: exceptions.h:360
int errnum() const
Return the error number corresponding to the error message returned by what(), if any.
Definition: exceptions.h:380
DBSelectionFailed(const char *w="", int e=0)
Create exception object.
Definition: exceptions.h:366
Base class for all MySQL++ custom exceptions.
Definition: exceptions.h:46
Exception & operator=(const Exception &rhs)
Assign another exception object's contents to this one.
Definition: exceptions.h:56
~Exception()
Destroy exception object.
Definition: exceptions.h:63
std::string what_
explanation of why exception was thrown
Definition: exceptions.h:85
Exception(const std::string &w)
Create exception object.
Definition: exceptions.h:79
Exception(const Exception &e) MAY_THROW()
Create exception object as copy of another.
Definition: exceptions.h:49
Exception(const char *w="")
Create exception object.
Definition: exceptions.h:73
virtual const char * what() const
Returns explanation of why exception was thrown.
Definition: exceptions.h:66
Exception thrown when a BeecryptMutex object fails.
Definition: exceptions.h:390
MutexFailed(const char *w="lock failed")
Create exception object.
Definition: exceptions.h:393
Exception thrown when you try to use an object that isn't completely initialized.
Definition: exceptions.h:404
ObjectNotInitialized(const char *w="")
Create exception object.
Definition: exceptions.h:407
Used within MySQL++'s test harness only.
Definition: exceptions.h:417
SelfTestFailed(const std::string &w)
Create exception object.
Definition: exceptions.h:420
Thrown from the C++ to SQL data type conversion routine when it can't figure out how to map the type.
Definition: exceptions.h:439
TypeLookupFailed(const std::string &w)
Create exception object.
Definition: exceptions.h:442
Exception thrown when something goes wrong in processing a "use" query.
Definition: exceptions.h:255
UseQueryError(const char *w="")
Create exception object.
Definition: exceptions.h:258
Declares the Option class hierarchy, used to implement connection options in Connection and DBDriver ...