Lumiera  0.pre.03
»edit your freedom«
exception-error-test.cpp
Go to the documentation of this file.
1 /*
2  ExceptionError(Test) - throwing and catching our exception type
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
29 #include "lib/error.h"
30 #include "lib/error.hpp"
31 #include "lib/test/run.hpp"
32 #include "lib/format-cout.hpp"
33 #include "lib/util.hpp"
34 
35 #include <exception>
36 #include <stdexcept>
37 #include <string>
38 
39 using std::runtime_error;
40 using std::exception;
41 using std::string;
42 
43 
44 
45 namespace lumiera {
46  namespace test {
47 
48  LUMIERA_ERROR_DEFINE (LIFE_AND_UNIVERSE, "and everything?");
52  LUMIERA_ERROR_DECLARE(DERIVED);
53  LUMIERA_ERROR_DEFINE (DERIVED, "convoluted exception");
54 
57 
58 
59  /******************************************************/
67  class ExceptionError_test : public Test
68  {
69  typedef ExceptionError_test test;
70 
71  virtual void
72  run (Arg)
73  {
75  catcher (&test::throwDerived, "test-1");
76  catcher (&test::throwFatal, "test-2");
77  catcher (&test::throwInvalid, "test-3");
78  catcher (&test::throwExternal, "test-4");
79  catcher (&test::throwRuntime, "test-5");
80  catcher (&test::throwExceptn, "test-6");
81 
82  catcher (&test::nestedThrower, "test-7");
83  catcher (&test::doubleNestedTh,"test-8");
84 
87  }
88 
89 
90 
92  void throwSpecial (string ) { throw SpecificError(); }
93  void throwDerived (string ) { throw DerivedError(); }
94  void throwFatal (string _) { throw error::Fatal(_); }
95  void throwInvalid (string _) { throw error::Invalid(_); }
96  void throwExternal(string _) { throw error::External(_); }
97  void throwRuntime (string _) { throw std::runtime_error(_); }
98  void throwExceptn (string ) { throw std::exception(); }
99 
100 
110  void nestedThrower (string msg)
111  {
112  try { throwExternal(msg); }
113  catch (std::exception& e)
114  {
115  cout << "intermediate handler caught: " << e.what()
116  << "....will rethrow as error::State\n";
117  throw error::State (e);
118  }
119  }
120 
122  void doubleNestedTh (string msg)
123  {
124  try { nestedThrower (msg); }
125  catch (Error& e)
126  {
127  cout << "2nd intermediate handler caught: " << e.what()
128  << "....will rethrow as error::Config\n";
129  throw error::Config (e);
130  }
131  }
132 
133 
134  void detectErrorflag (string) { throwOnError(); }
135 
136 
141  {
142  lumiera_error_set(LUMIERA_ERROR_LIFE_AND_UNIVERSE, "what is the answer?");
143  CHECK (lumiera_error_peek());
144 
145  catcher (&test::detectErrorflag);
146  CHECK (not lumiera_error_peek());
147  }// yet translating that into an exception also clears the error flag
148 
149 
154  {
155  error::Logic err1;
156  error::Config err2(err1);
157  error::Config err3(err2);
158  Error err4(err1); // note: copy ctor
159 
160  std::runtime_error rerr("what a shame");
161  error::External err5(rerr);
162  Error err6(err5);
163 
164  CHECK (err2.rootCause() == err1.what());
165  CHECK (err3.rootCause() == err1.what());
166  CHECK (err4.rootCause() == err1.rootCause()); // mere copy is not a root cause
167 
168  CHECK (err5.rootCause() == rerr.what());
169  CHECK (err6.rootCause() == rerr.what());
170  }
171 
172 
178  : public error::Invalid
179  {
180  int value_;
181  public:
182  SpecificError()
183  : error::Invalid{"don't panic", LUMIERA_ERROR_LIFE_AND_UNIVERSE}
184  , value_(42)
185  { }
186 
187  int
188  revealIt()
189  {
190  return value_;
191  }
192  };
193 
194 
195 
199  void catcher (void (ExceptionError_test::*funky)(string), string context ="")
200  {
201  try
202  {
203  (this->*funky) (context);
204  }
205 
206  catch (SpecificError& e) { cout << "caught: " << e.what() << "..the answer is: " << e.revealIt() << "\n"; }
207  catch (error::Logic& e) { cout << "caught error::Logic: " << e.what() << "\n"; }
208  catch (error::Invalid&e) { cout << "caught error::Invalid: " << e.what() << "\n"; }
209  catch (Error& e) { cout << "caught lumiera::Error: " << e.what() << "\n"; }
210  catch (runtime_error& e) { cout << "caught std::runtime_error: " << e.what() << "\n"; }
211  catch (exception& e) { cout << "caught std::exception. (unspecific)" << "\n"; }
212  catch (...) { cout << "caught an unknown exception\n"; }
213  }
214  };
215 
216 
217 
219  LAUNCHER (ExceptionError_test, "function common");
220 
221 
222  } // namespace test
223 
224 } // namespace util
225 
lumiera_err lumiera_error_peek(void)
Check current error state without clearing it Please avoid this function and use lumiera_error() if p...
Definition: error-state.c:142
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
virtual CStr what() const noexcept override
std::exception interface : yield a diagnostic message
void catcher(void(ExceptionError_test::*funky)(string), string context="")
helper: provides a bunch of catch-clauses and runs the given member functions within ...
void throwOnError()
Check the lumiera error state, which maybe was set by C-code.
Definition: error.hpp:242
#define LUMIERA_ERROR_DECLARE(err)
Forward declare an error constant.
Definition: error.h:71
Some aspects of C++ exception handling.
Lumiera error handling (C interface).
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
string const & rootCause() const noexcept
If this exception was caused by a chain of further exceptions, return the description of the first on...
Definition: error.hpp:122
Lumiera error handling (C++ interface).
lumiera_err lumiera_error_set(lumiera_err nerr, const char *extra)
Set error state for the current thread.
Definition: error-state.c:105
Lumiera public interface.
Definition: advice.cpp:113
a very specific Exception class local to this scope and with additional behaviour.
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:71
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition: error.h:80