TF++, Miscellaneous classes and modules in C++:
exceptiontest.cc
Go to the documentation of this file.
1 
37 #define EXCEPTIONTEST_VERSION \
38  "EXCEPTIONTEST V1.0 test exception classes"
39 
40 #include <iostream>
41 #include <tfxx/commandline.h>
42 #include <tfxx/error.h>
43 #include <tfxx/fs.h>
44 
45 using std::cout;
46 using std::cerr;
47 using std::endl;
48 
49 /*======================================================================*/
50 
51 void throwb()
52 {
53  throw tfxx::error::FSException("simulate fs error",
54  __FILE__, __LINE__, 5);
55 }
56 
57 void throwa()
58 {
59  TFXX_abort("abort anyway");
60 }
61 
62 class A {
63  public:
64  A(const bool& throwbase): Mtb(throwbase) { }
65  void setflag(const bool& tb) { Mtb=tb; }
66  void throwit() const
67  {
68  if (Mtb)
69  {
70  throwa();
71  }
72  else
73  {
74  throwb();
75  }
76  }
77  private:
78  bool Mtb;
79 };
80 
81 struct Options {
84 };
85 
86 /*======================================================================*/
87 
88 int main(int iargc, char* argv[])
89 {
90 
91  // define usage information
92  char usage_text[]=
93  {
95  "usage: exceptiontest [-b] [-nr] [-roc]" "\n"
96  " or: exceptiontest --help|-h" "\n"
97  };
98 
99  // define full help text
100  char help_text[]=
101  {
102  EXCEPTIONTEST_CVSID
103  "\n"
104  "-b throw base class\n"
105  "-nr do not resume\n"
106  "-roc report on construct\n"
107  };
108 
109  // define commandline options
110  using namespace tfxx::cmdline;
111  static Declare options[]=
112  {
113  // 0: print help
114  {"help",arg_no,"-"},
115  // 1: verbose mode
116  {"v",arg_no,"-"},
117  // 2: throw base class
118  {"b",arg_no,"-"},
119  // 3: no resume
120  {"nr",arg_no,"-"},
121  // 4: no resume
122  {"roc",arg_no,"-"},
123  {NULL}
124  };
125 
126  // no arguments? print usage...
127  if (iargc<1)
128  {
129  cerr << usage_text << endl;
130  exit(0);
131  }
132 
133  // collect options from commandline
134  Commandline cmdline(iargc, argv, options);
135 
136  // help requested? print full help text...
137  if (cmdline.optset(0))
138  {
139  cerr << usage_text << endl;
140  cerr << help_text << endl;
141  exit(0);
142  }
143 
144  Options opt;
145  opt.throwbase=cmdline.optset(2);
146  opt.noresume=cmdline.optset(3);
147  opt.reportonconstruct=cmdline.optset(4);
148 
149  /*
150  // dummy operation: print option settings
151  for (int iopt=0; iopt<2; iopt++)
152  {
153  cout << "option: '" << options[iopt].opt_string << "'" << endl;
154  if (cmdline.optset(iopt)) { cout << " option was set"; }
155  else { cout << "option was not set"; }
156  cout << endl;
157  cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
158  cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
159  cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
160  cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
161  cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
162  cout << " argument (bool): '";
163  if (cmdline.bool_arg(iopt))
164  { cout << "true"; } else { cout << "false"; }
165  cout << "'" << endl;
166  }
167  while (cmdline.extra()) { cout << cmdline.next() << endl; }
168 
169  // dummy operation: print rest of command line
170  while (cmdline.extra()) { cout << cmdline.next() << endl; }
171  */
172 
173  A a(opt.throwbase);
174 
175  if (opt.reportonconstruct)
176  {
178  }
179  else
180  {
182  }
183 
184  try {
185  a.throwit();
186  } catch (tfxx::error::Exception& e) {
187  cout << "caught exception" << endl;
188  e.report();
189  if (opt.noresume)
190  {
191  throw(e);
192  }
193  }
194 
195  cout << "again" << endl;
197 
198  try {
199  a.throwit();
200  } catch (tfxx::error::Exception& e) {
201  cout << "caught exception" << endl;
202  e.report();
203  if (opt.noresume)
204  {
205  throw(e);
206  }
207  }
208 
209  cout << "survived" << endl;
210 }
211 
212 /* ----- END OF exceptiontest.cc ----- */
#define EXCEPTIONTEST_VERSION
exception class for file system utilities
Definition: fs.h:72
bool optset(const int &iopt) const
true if option # iopt was set on commandline
Definition: commandline.h:213
static void restore_report_state()
Restore previous report state.
Definition: error.cc:98
void throwa()
static void dont_report_on_construct()
Issue NO screen report on construction of exception.
Definition: error.cc:91
bool Mtb
A(const bool &throwbase)
Namespace containing all components of module commandline. ,.
Definition: commandline.cc:41
void throwit() const
Evaluates commandline by calling long_getopt. ,You may instantiate a Commandline object by passing th...
Definition: commandline.h:201
void throwb()
static void report_on_construct()
Issue a screen report on construction of exception.
Definition: error.cc:84
int main(int iargc, char *argv[])
bool throwbase
struct to define options ,This struct is used to define a list of options. An example is: ...
Definition: commandline.h:136
void setflag(const bool &tb)
option has no argument
Definition: commandline.h:100
bool reportonconstruct
Base class for exceptions.
Definition: error.h:78
virtual void report() const
Screen report.
Definition: error.cc:108
bool noresume
#define TFXX_abort(M)
Abort and give a message.
Definition: error.h:183