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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
20 #include "lib/error.h"
21 #include "lib/error.hpp"
22 #include "lib/test/run.hpp"
23 #include "lib/format-cout.hpp"
24 #include "lib/util.hpp"
25 
26 #include <exception>
27 #include <stdexcept>
28 #include <string>
29 
30 using std::runtime_error;
31 using std::exception;
32 using std::string;
33 
34 
35 
36 namespace lumiera {
37  namespace test {
38 
39  LUMIERA_ERROR_DEFINE (LIFE_AND_UNIVERSE, "and everything?");
43  LUMIERA_ERROR_DECLARE(DERIVED);
44  LUMIERA_ERROR_DEFINE (DERIVED, "convoluted exception");
45 
48 
49 
50  /******************************************************/
58  class ExceptionError_test : public Test
59  {
60  typedef ExceptionError_test test;
61 
62  virtual void
63  run (Arg)
64  {
66  catcher (&test::throwDerived, "test-1");
67  catcher (&test::throwFatal, "test-2");
68  catcher (&test::throwInvalid, "test-3");
69  catcher (&test::throwExternal, "test-4");
70  catcher (&test::throwRuntime, "test-5");
71  catcher (&test::throwExceptn, "test-6");
72 
73  catcher (&test::nestedThrower, "test-7");
74  catcher (&test::doubleNestedTh,"test-8");
75 
78  }
79 
80 
81 
83  void throwSpecial (string ) { throw SpecificError(); }
84  void throwDerived (string ) { throw DerivedError(); }
85  void throwFatal (string _) { throw error::Fatal(_); }
86  void throwInvalid (string _) { throw error::Invalid(_); }
87  void throwExternal(string _) { throw error::External(_); }
88  void throwRuntime (string _) { throw std::runtime_error(_); }
89  void throwExceptn (string ) { throw std::exception(); }
90 
91 
101  void nestedThrower (string msg)
102  {
103  try { throwExternal(msg); }
104  catch (std::exception& e)
105  {
106  cout << "intermediate handler caught: " << e.what()
107  << "....will rethrow as error::State\n";
108  throw error::State (e);
109  }
110  }
111 
113  void doubleNestedTh (string msg)
114  {
115  try { nestedThrower (msg); }
116  catch (Error& e)
117  {
118  cout << "2nd intermediate handler caught: " << e.what()
119  << "....will rethrow as error::Config\n";
120  throw error::Config (e);
121  }
122  }
123 
124 
125  void detectErrorflag (string) { throwOnError(); }
126 
127 
132  {
133  lumiera_error_set(LUMIERA_ERROR_LIFE_AND_UNIVERSE, "what is the answer?");
134  CHECK (lumiera_error_peek());
135 
136  catcher (&test::detectErrorflag);
137  CHECK (not lumiera_error_peek());
138  }// yet translating that into an exception also clears the error flag
139 
140 
145  {
146  error::Logic err1;
147  error::Config err2(err1);
148  error::Config err3(err2);
149  Error err4(err1); // note: copy ctor
150 
151  std::runtime_error rerr("what a shame");
152  error::External err5(rerr);
153  Error err6(err5);
154 
155  CHECK (err2.rootCause() == err1.what());
156  CHECK (err3.rootCause() == err1.what());
157  CHECK (err4.rootCause() == err1.rootCause()); // mere copy is not a root cause
158 
159  CHECK (err5.rootCause() == rerr.what());
160  CHECK (err6.rootCause() == rerr.what());
161  }
162 
163 
169  : public error::Invalid
170  {
171  int value_;
172  public:
173  SpecificError()
174  : error::Invalid{"don't panic", LUMIERA_ERROR_LIFE_AND_UNIVERSE}
175  , value_(42)
176  { }
177 
178  int
179  revealIt()
180  {
181  return value_;
182  }
183  };
184 
185 
186 
190  void catcher (void (ExceptionError_test::*funky)(string), string context ="")
191  {
192  try
193  {
194  (this->*funky) (context);
195  }
196 
197  catch (SpecificError& e) { cout << "caught: " << e.what() << "..the answer is: " << e.revealIt() << "\n"; }
198  catch (error::Logic& e) { cout << "caught error::Logic: " << e.what() << "\n"; }
199  catch (error::Invalid&e) { cout << "caught error::Invalid: " << e.what() << "\n"; }
200  catch (Error& e) { cout << "caught lumiera::Error: " << e.what() << "\n"; }
201  catch (runtime_error& e) { cout << "caught std::runtime_error: " << e.what() << "\n"; }
202  catch (exception& e) { cout << "caught std::exception. (unspecific)" << "\n"; }
203  catch (...) { cout << "caught an unknown exception\n"; }
204  }
205  };
206 
207 
208 
210  LAUNCHER (ExceptionError_test, "function common");
211 
212 
213  } // namespace test
214 
215 } // namespace util
216 
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:133
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:40
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:233
#define LUMIERA_ERROR_DECLARE(err)
Forward declare an error constant.
Definition: error.h:62
Some aspects of C++ exception handling.
Lumiera error handling (C interface).
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Simplistic 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:113
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:96
Lumiera public interface.
Definition: advice.cpp:104
a very specific Exception class local to this scope and with additional behaviour.
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:62
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition: error.h:71