Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
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
30using std::runtime_error;
31using std::exception;
32using std::string;
33
34
35
36namespace lumiera {
37 namespace test {
38
39 LUMIERA_ERROR_DEFINE (LIFE_AND_UNIVERSE, "and everything?");
44 LUMIERA_ERROR_DEFINE (DERIVED, "convoluted exception");
45
48
49
50 /******************************************************/
58 class ExceptionError_test : public Test
59 {
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
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 {
172 public:
174 : error::Invalid{"don't panic", LUMIERA_ERROR_LIFE_AND_UNIVERSE}
175 , value_(42)
176 { }
177
178 int
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
Interface and Base definition for all Lumiera Exceptions.
Definition error.hpp:65
virtual CStr what() const noexcept override
std::exception interface : yield a diagnostic message
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:114
Derived specific exceptions within Lumiera's exception hierarchy.
Definition error.hpp:193
a very specific Exception class local to this scope and with additional behaviour.
Some aspects of C++ exception handling.
void catcher(void(ExceptionError_test::*funky)(string), string context="")
helper: provides a bunch of catch-clauses and runs the given member functions within
lumiera_err lumiera_error_peek(void)
Check current error state without clearing it Please avoid this function and use lumiera_error() if p...
lumiera_err lumiera_error_set(lumiera_err nerr, const char *extra)
Set error state for the current thread.
Definition error-state.c:96
Lumiera error handling (C interface).
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition error.h:71
#define LUMIERA_ERROR_DECLARE(err)
Forward declare an error constant.
Definition error.h:62
Lumiera error handling (C++ interface).
Automatically use custom string conversion in C++ stream output.
#define _(String)
Definition gtk-base.hpp:68
LumieraError< LERR_(STATE)> State
Definition error.hpp:209
LumieraError< LERR_(FATAL), Logic > Fatal
Definition error.hpp:208
LumieraError< LERR_(LOGIC)> Logic
Definition error.hpp:207
LumieraError< LERR_(CONFIG), Invalid > Config
Definition error.hpp:212
LumieraError< LERR_(INVALID)> Invalid
Definition error.hpp:211
LumieraError< LERR_(EXTERNAL)> External
Definition error.hpp:213
error::LumieraError< LUMIERA_ERROR_DERIVED, error::External > DerivedError
define a specific Error subclass derived from error::external
Lumiera public interface.
Definition advice.hpp:102
void throwOnError()
Check the lumiera error state, which maybe was set by C-code.
Definition error.hpp:234
Test runner and basic definitions for tests.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...