Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JThrow.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JTHROW__
2 #define __JLANG__JTHROW__
3 
4 #include <iostream>
5 
6 #include "JLang/JException.hh"
7 
8 
9 /**
10  * \file
11  *
12  * Exception handling.
13  * \author mdejong
14  */
15 namespace JLANG {}
16 namespace JPP { using namespace JLANG; }
17 
18 namespace JLANG {
19 
20  /**
21  * Auxiliary base class for controling the throwing of exceptions.
22  * The template class refers to the throwing object.
23  */
24  template<class T>
25  class JThrow {
26  protected:
27 
28  static bool do_throw; //!< throw option
29 
30 
31  public:
32  /**
33  * Enable/disable throw option.
34  *
35  * \param option true enable; false disable
36  */
37  static void Throw(const bool option)
38  {
39  do_throw = option;
40  }
41 
42 
43  /**
44  * Throw exception or return error.
45  *
46  * \param error exception
47  * \param value return code
48  * \return return code
49  */
50  static int Throw(const JException& error, const int value = -1)
51  {
52  using namespace std;
53 
54  if (do_throw) {
55  throw error;
56  }
57 
58  cerr << error.what() << endl;
59 
60  return value;
61  }
62  };
63 
64 
65  /**
66  * Set default throw option to <tt>true</tt>.
67  */
68  template<class T>
69  bool JThrow<T>::do_throw = true;
70 }
71 
72 #endif
General exception.
Definition: JException.hh:23
Exceptions.
Auxiliary base class for controling the throwing of exceptions.
Definition: JThrow.hh:25
static int Throw(const JException &error, const int value=-1)
Throw exception or return error.
Definition: JThrow.hh:50
virtual const char * what() const override
Get error message.
Definition: JException.hh:48
static bool do_throw
throw option
Definition: JThrow.hh:28
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37