KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
l_exception.h
Go to the documentation of this file.
1 
11 /*
12  * $Id: l_exception.h 18250 2014-11-20 00:55:38Z ksimek $
13  */
14 
15 /* =========================================================================== *
16 |
17 | Copyright (c) 1994-2010
18 |
19 | Personal and educational use of this code is granted, provided that this
20 | header is kept intact, and that the authorship is not misrepresented, that
21 | its use is acknowledged in publications, and relevant papers are cited.
22 |
23 | For other use contact the author (kobus AT cs DOT arizona DOT edu).
24 |
25 | Please note that the code in this file has not necessarily been adequately
26 | tested. Naturally, there is no guarantee of performance, support, or fitness
27 | for any particular task. Nonetheless, I am interested in hearing about
28 | problems that you encounter.
29 |
30 | Authors:
31 | Kobus Barnard, Kyle Simek, Joseph Schlecht, Luca Del Pero, Andrew Predoehl
32 |
33 * =========================================================================== */
34 
35 #ifndef KJB_EXCEPTION_H
36 #define KJB_EXCEPTION_H
37 
38 #include <l/l_sys_err.h>
39 #include <l/l_sys_str.h>
40 #include <l/l_string.h>
41 #include <boost/preprocessor/seq.hpp>
42 #include <exception>
43 #include <string>
44 #include <iosfwd>
45 
46 #define KJB_THROW(ex) throw ex(__FILE__, __LINE__)
47 
48 #define KJB_THROW_2(ex, msg) throw ex(msg, __FILE__, __LINE__)
49 
50 /*
51  * The do { ... } while(0) structure below avoids the mistake known as
52  * "swallowing the semicolon."
53  */
54 
55 // usage: KJB_THROW_3(Exception, format_string, (param1)(param2)(param3)(...))
56 #define KJB_THROW_3(ex, fmt, params) \
57  do \
58  { \
59  char buffer[ERROR_MESS_BUFF_SIZE]; \
60  kjb_c::kjb_sprintf(buffer, ERROR_MESS_BUFF_SIZE, fmt, \
61  BOOST_PP_SEQ_ENUM(params)); \
62  throw ex(buffer, __FILE__, __LINE__); \
63  } \
64  while(0)
65 
66 // "On error throw exception" -- in this case, "error" is ANY nonzero value.
67 #define ETX(a) \
68  do \
69  { \
70  if (a) \
71  { \
72  kjb::throw_kjb_error( "", __FILE__, __LINE__ ); \
73  } \
74  } \
75  while(0)
76 
77 
78 #define ETX_2(a, msg) \
79  do \
80  { \
81  if (a) \
82  { \
83  kjb::throw_kjb_error(msg, __FILE__, __LINE__); \
84  } \
85  } \
86  while(0)
87 
88 // "On null throw exception"
89 #define NTX(a) \
90  do \
91  { \
92  if ((a) == NULL) \
93  { \
94  kjb::throw_kjb_error( "Null result", \
95  __FILE__, __LINE__ ); \
96  } \
97  } \
98  while(0)
99 
100 // "If False Throw (exception)" -- throws 'ex' if 'a' is false.
101 #define IFT(a, ex, msg) \
102  do \
103  { \
104  if(!(a)) \
105  { \
106  KJB_THROW_2(ex, msg); \
107  } \
108  } \
109  while(0) \
110 
111 // "If False Throw (exception) with details" -- throws 'ex' if 'a' is false.
112 #define IFTD(a, ex, msg, params) \
113  do \
114  { \
115  if(!(a)) \
116  { \
117  KJB_THROW_3(ex, msg, params); \
118  } \
119  } \
120  while(0) \
121 
122 
123 
124 
125 namespace kjb {
126 
132 class Exception : public std::exception
133 {
134 public:
135 
137  Exception(const char* msg, const char* file=0, unsigned line=0);
138 
139 
141  Exception(const std::string& msg, const char* file=0, unsigned line=0);
142 
144  Exception(const Exception& e);
145 
146 
148  virtual ~Exception() throw() { }
149 
150 
152  const std::string& get_msg() const throw()
153  {
154  return m_msg;
155  }
156 
158  const char* what() const throw()
159  {
160  return m_msg.c_str();
161  }
162 
163 
165  const char* get_file() const
166  {
167  return m_file;
168  }
169 
170 
172  unsigned get_line() const
173  {
174  return m_line;
175  }
176 
178  std::string get_details() const;
179 
180 
182  virtual void print(
183  std::ostream& out, // = std::cerr
184  bool newline = false
185  ) const;
186 
187  virtual void print() const;
188 
189 
190 
192  virtual void print_details(
193  std::ostream& out, // = std::cerr
194  bool newline = false
195  ) const;
196 
197  virtual void print_details() const;
198 
200  virtual void print_abort(
201  std::ostream& out, // = std::cerr
202  bool newline = false
203  ) const;
204 
205  virtual void print_abort() const;
206 
211  virtual void print_details_abort
212  (
213  std::ostream& out, // = std::cerr
214  bool newline = false
215  )
216  const;
217 
218  virtual void print_details_abort() const;
219 
220 
222  virtual void print_exit
223  (
224  std::ostream& out,
225  bool newline, // = std::cerr
226  int status = false
227  )
228  const;
229 
230  virtual void print_exit() const;
231 
232 
237  virtual void print_details_exit
238  (
239  std::ostream& out,
240  bool newline = false,
241  int status = EXIT_FAILURE
242  )
243  const;
244 
245  virtual void print_details_exit() const;
246 
247 
248 protected:
249 
251  std::string m_msg;
252 
254  const char* m_file;
255 
257  unsigned m_line;
258 };
259 
260 
262 class KJB_error : public Exception
263 {
264 public:
266  KJB_error(const char* msg, const char* file=0, unsigned line=0);
267 
268 
270  KJB_error(const std::string& msg, const char* file=0, unsigned line=0);
271 
272 
274  virtual ~KJB_error() throw() { }
275 
276 };
277 
278 
279 
282 {
283 public:
286  Not_implemented(const char* file, unsigned line);
287 
290  Not_implemented(const char* msg, const char* file=0, unsigned line=0);
291 
292 
296  const std::string& msg,
297  const char* file=0,
298  unsigned line=0
299  );
300 
301 
303  virtual ~Not_implemented() throw() { }
304 
305 };
306 
321 class Runtime_error : public Exception
322 {
323 public:
325  Runtime_error(const char* file, unsigned line);
326 
328  Runtime_error(const char* msg, const char* file=0, unsigned line=0);
329 
330 
333  const std::string& msg,
334  const char* file=0,
335  unsigned line=0
336  );
337 
339  virtual ~Runtime_error() throw() { }
340 
341 };
342 
358 class Cant_happen : public Exception
359 {
360 public:
362  Cant_happen(const char* file, unsigned line);
363 
365  Cant_happen(const char* msg, const char* file=0, unsigned line=0);
366 
368  Cant_happen(const std::string& msg, const char* file=0, unsigned line=0);
369 
371  virtual ~Cant_happen() throw() { }
372 
373 };
374 
375 
378 {
379 public:
381  Illegal_argument(const char* file, unsigned line);
382 
384  Illegal_argument(const char* msg, const char* file=0, unsigned line=0);
385 
388  const std::string& msg,
389  const char* file=0,
390  unsigned line=0
391  );
392 
394  virtual ~Illegal_argument() throw() { }
395 };
396 
397 
400 {
401 public:
404  Index_out_of_bounds(const char* file, unsigned line);
405 
408  Index_out_of_bounds(const char* msg, const char* file=0, unsigned line=0);
409 
410 
414  const std::string& msg,
415  const char* file=0,
416  unsigned line=0
417  );
418 
419 
421  virtual ~Index_out_of_bounds() throw() { }
422 };
423 
424 
427 {
428 public:
430  Dimension_mismatch(const char* file, unsigned line);
431 
433  Dimension_mismatch(const char* msg, const char* file=0, unsigned line=0);
434 
435 
438  const std::string& msg,
439  const char* file=0,
440  unsigned line=0
441  );
442 
444  virtual ~Dimension_mismatch() throw() { }
445 
446 };
447 
448 
451 {
452 public:
454  Divide_by_zero(const char* file, unsigned line);
455 
457  Divide_by_zero(const char* msg, const char* file=0, unsigned line=0);
458 
459 
462  const std::string& msg,
463  const char* file=0,
464  unsigned line=0
465  );
466 
468  virtual ~Divide_by_zero() throw() { }
469 
470 };
471 
474 {
475 public:
477  Serialization_error(const char* file, unsigned line);
478 
480  Serialization_error(const char* msg, const char* file=0, unsigned line=0);
481 
482 
485  const std::string& msg,
486  const char* file=0,
487  unsigned line=0
488  );
489 
491  virtual ~Serialization_error() throw() { }
492 
493 };
494 
496 class IO_error : public Exception
497 {
498 public:
500  IO_error(const char* file, unsigned line);
501 
503  IO_error(const char* msg, const char* file=0, unsigned line=0);
504 
505 
507  IO_error(const std::string& msg, const char* file=0, unsigned line=0);
508 
509 
511  virtual ~IO_error() throw() { }
512 
513 };
514 
516 class Result_error : public Exception
517 {
518 public:
521  Result_error(const char* file, unsigned line);
522 
525  Result_error(const char* msg, const char* file=0, unsigned line=0);
526 
527 
530  Result_error(const std::string& msg, const char* file=0, unsigned line=0);
531 
532 
534  virtual ~Result_error() throw() { }
535 
536 };
537 
540 {
541 public:
543  Missing_dependency(const char* file, unsigned line);
544 
547  const char* dependency,
548  const char* file=0,
549  unsigned line=0
550  );
551 
552 
555  const std::string& dependency,
556  const char* file=0,
557  unsigned line=0
558  );
559 
560 
562  virtual ~Missing_dependency() throw() { }
563 
564 };
565 
568 {
569 public:
572  const std::string& msg,
573  const char* file=0,
574  unsigned line=0
575  );
576 
578  virtual ~Option_exception() throw() { }
579 };
580 
583 {
584 public:
587  const std::string& option,
588  const char* file=0,
589  unsigned line=0
590  );
591 
593  virtual ~Missing_option() throw() { }
594 };
595 
598 #if 0
600 {
601 public:
604  Cant_happen_exception(const char* file=0, unsigned line=0);
605 
607  virtual ~Cant_happen_exception() { } throw()
608 };
609 #endif
610 
613 {
614 public:
616  Stack_overflow(const char* file=0, unsigned line=0);
617 
619  virtual ~Stack_overflow() throw() { }
620 };
621 
624 {
625 public:
627  Stack_underflow(const char* file=0, unsigned line=0);
628 
630  virtual ~Stack_underflow() throw() { }
631 };
632 
633 
636 {
637 public:
639  Resource_exhaustion(const char* file=0, unsigned line=0)
640  : Runtime_error("Resource exhaustion", file, line)
641  {}
642 
644  virtual ~Resource_exhaustion() throw() {}
645 };
646 
647 
648 std::string kjb_get_error();
649 
650 void throw_kjb_error( const char* msg, const char* file, unsigned line);
651 
652 } // namespace kjb
653 
654 
655 #endif
Object thrown when serialization or deserialization fails.
Definition: l_exception.h:473
Base class of all exceptions in the jwsc++ library.
Definition: l_exception.h:132
const char * get_file() const
Returns the file name where the Exception occurred.
Definition: l_exception.h:165
virtual ~Resource_exhaustion()
Deletes exception.
Definition: l_exception.h:644
Object thrown when an index argument exceeds the size of a container.
Definition: l_exception.h:399
virtual ~Missing_option()
Deletes exception.
Definition: l_exception.h:593
Serialization_error(const char *file, unsigned line)
Constructs an Exception caused by a serialization error.
Definition: l_exception.cpp:527
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
virtual ~Option_exception()
Deletes exception.
Definition: l_exception.h:578
std::string get_details() const
Return string containing filename, line number, and message.
Definition: l_exception.cpp:80
unsigned get_line() const
Returns the line number near where the Exception occurred.
Definition: l_exception.h:172
virtual ~Exception()
Deletes an Exception.
Definition: l_exception.h:148
Object thrown for exceptions related to command-line options.
Definition: l_exception.h:567
virtual ~Result_error()
Deletes a Result error.
Definition: l_exception.h:534
Object thrown when a function cannot generate a valid result.
Definition: l_exception.h:516
Object thrown if a resource allocation failed (memory, fp, etc.)
Definition: l_exception.h:635
Object thrown if stack is empty and popped.
Definition: l_exception.h:623
virtual ~Runtime_error()
Deletes a Runtime_error.
Definition: l_exception.h:339
void throw_kjb_error(const char *msg, const char *file, unsigned line)
Definition: l_exception.cpp:748
Runtime_error(const char *file, unsigned line)
Constructs a runtime exception.
Definition: l_exception.cpp:324
virtual void print_details() const
Definition: l_exception.cpp:119
const std::string & get_msg() const
Returns the error message for the Exception.
Definition: l_exception.h:152
virtual ~KJB_error()
Deletes a KJB_error.
Definition: l_exception.h:274
virtual void print_abort() const
Definition: l_exception.cpp:137
Result_error(const char *file, unsigned line)
Constructs an Exception caused by a function generating an invalid result.
Definition: l_exception.cpp:605
const char * what() const
Returns the error message for the Exception.
Definition: l_exception.h:158
Index_out_of_bounds(const char *file, unsigned line)
Constructs an Exception caused by attempting to access an index outside the bounds of a collection...
Definition: l_exception.cpp:236
KJB_error(const char *msg, const char *file=0, unsigned line=0)
Constructs Exception caused by an error in the KJB C library.
Definition: l_exception.cpp:210
Stack_underflow(const char *file=0, unsigned line=0)
Constructs an Exception caused by a stack underflow.
Definition: l_exception.cpp:714
virtual ~Cant_happen()
Deletes a Cant_happen.
Definition: l_exception.h:371
virtual void print_details_abort() const
Definition: l_exception.cpp:155
virtual ~Divide_by_zero()
Deletes a Divide_by_zero.
Definition: l_exception.h:468
Resource_exhaustion(const char *file=0, unsigned line=0)
Constructs an Exception caused by a stack underflow. */.
Definition: l_exception.h:639
Missing_dependency(const char *file, unsigned line)
Constructs an Exception caused by a missing dependency.
Definition: l_exception.cpp:636
Dimension_mismatch(const char *file, unsigned line)
Constructs an Exception caused by dimension mismatch.
Definition: l_exception.cpp:445
Option_exception(const std::string &msg, const char *file=0, unsigned line=0)
Constructs an Exception related to command-line options.
Definition: l_exception.cpp:672
Exception often thrown when wrapped C functions return error codes.
Definition: l_exception.h:262
Stack_overflow(const char *file=0, unsigned line=0)
Constructs an Exception caused by a stack overflow.
Definition: l_exception.cpp:707
Cant_happen Cant_happen_exception
Definition: l_exception.h:597
Object thrown when an division is attempted with a zero divisor.
Definition: l_exception.h:450
Object thrown when an obligatory command-line option is absent.
Definition: l_exception.h:582
virtual ~Stack_underflow()
Deletes exception.
Definition: l_exception.h:630
virtual ~IO_error()
Deletes an IO error.
Definition: l_exception.h:511
unsigned m_line
Line number where the Error occurred.
Definition: l_exception.h:257
virtual ~Missing_dependency()
Deletes exception.
Definition: l_exception.h:562
virtual void print() const
Definition: l_exception.cpp:100
virtual void print_exit() const
Definition: l_exception.cpp:175
virtual void print_details_exit() const
Definition: l_exception.cpp:196
Object thrown when the program does something thought impossible.
Definition: l_exception.h:358
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
virtual ~Dimension_mismatch()
Deletes a Dimension_mismatch.
Definition: l_exception.h:444
Object thrown when attempting to use unimplemented functionality.
Definition: l_exception.h:281
Divide_by_zero(const char *file, unsigned line)
Constructs an Exception caused by dividing by zero.
Definition: l_exception.cpp:488
std::string m_msg
Message associated with the error causing the exception.
Definition: l_exception.h:251
Object thrown when input or output fails.
Definition: l_exception.h:496
Cant_happen(const char *file, unsigned line)
Ctor of Exception caused reaching presumably unreachable code.
Definition: l_exception.cpp:362
virtual ~Serialization_error()
Deletes a Serialization_error.
Definition: l_exception.h:491
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
Missing_option(const std::string &option, const char *file=0, unsigned line=0)
Constructs an Exception caused by a missing command-line option.
Definition: l_exception.cpp:683
Not_implemented(const char *file, unsigned line)
Constructs an Exception caused by attempting to use un-implemented functionality. ...
Definition: l_exception.cpp:280
Illegal_argument(const char *file, unsigned line)
Ctor for passing an illegal argument to a method.
Definition: l_exception.cpp:401
Exception(const char *msg, const char *file=0, unsigned line=0)
Constructs an Exception.
Definition: l_exception.cpp:43
virtual ~Stack_overflow()
Deletes exception.
Definition: l_exception.h:619
virtual ~Index_out_of_bounds()
Deletes a Index_out_of_bounds.
Definition: l_exception.h:421
virtual ~Illegal_argument()
Deletes a Illegal_argument. */.
Definition: l_exception.h:394
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321
IO_error(const char *file, unsigned line)
Constructs an Exception caused by an IO error.
Definition: l_exception.cpp:570
Object thrown if a finite size stack is full and pushed farther.
Definition: l_exception.h:612
const char * m_file
File name where the Error occurred.
Definition: l_exception.h:254
std::string kjb_get_error()
similar to kjb_c::kjb_get_error(), but this returns std::string.
Definition: l_exception.cpp:723
virtual ~Not_implemented()
Deletes a Not_implemented.
Definition: l_exception.h:303