exceptions.cpp
Go to the documentation of this file.
1 
13 /*****************************************************************************
14 ** Includes
15 *****************************************************************************/
16 
17 #include <cstdio> // fopen
18 #include <iostream>
19 #include <ecl/config/ecl.hpp>
20 #include "../../include/ecl/exceptions/exception.hpp"
21 #include "../../include/ecl/exceptions/data_exception.hpp"
22 #include "../../include/ecl/exceptions/macros.hpp"
23 #include "../../include/ecl/exceptions/standard_exception.hpp"
24 
25 /*****************************************************************************
26 ** Disable check
27 *****************************************************************************/
28 
29 #include <ecl/config/ecl.hpp>
30 #ifndef ECL_DISABLE_EXCEPTIONS
31 
32 /*****************************************************************************
33 ** Using
34 *****************************************************************************/
35 
36 using namespace ecl;
37 
38 /*****************************************************************************
39 ** Functions
40 *****************************************************************************/
41 
42 void f(int i)
43 {
44  if ( i > 5 ) {
45  ecl_debug_throw(StandardException(LOC,InvalidInputError,"Debug throw test failed, input integer exceeded."));
46  } else {
47 // cout << "Valid integer supplied." << endl;
48  }
49 }
50 
51 /*****************************************************************************
52 ** Main
53 *****************************************************************************/
54 
55 int main(int argc, char **argv) {
56 
57  std::cout << std::endl;
58  std::cout << "***********************************************************" << std::endl;
59  std::cout << " Standard Exceptions" << std::endl;
60  std::cout << "***********************************************************" << std::endl;
61  std::cout << std::endl;
62 
63  try {
65  } catch ( StandardException &e ) {
66  std::cout << e.what() << std::endl;
67  }
68  try {
70  } catch ( StandardException &e ) {
71  std::cout << e.what() << std::endl;
72  }
73  try {
75  } catch ( StandardException &e ) {
76  std::cout << e.what() << std::endl;
77  }
78  try {
80  } catch ( StandardException &e ) {
81  std::cout << e.what() << std::endl;
82  }
83  try {
85  } catch ( StandardException &e ) {
86  std::cout << e.what() << std::endl;
87  }
88  try {
90  } catch ( StandardException &e ) {
91  std::cout << e.what() << std::endl;
92  }
93  try {
95  } catch ( StandardException &e ) {
96  std::cout << e.what() << std::endl;
97  }
98  try {
100  } catch ( StandardException &e ) {
101  std::cout << e.what() << std::endl;
102  }
103  try {
105  } catch ( StandardException &e ) {
106  std::cout << e.what() << std::endl;
107  }
108  try {
110  } catch ( StandardException &e ) {
111  std::cout << e.what() << std::endl;
112  }
113  try {
115  } catch ( StandardException &e ) {
116  std::cout << e.what() << std::endl;
117  }
118  try {
120  } catch ( StandardException &e ) {
121  std::cout << e.what() << std::endl;
122  }
123  try {
125  } catch ( StandardException &e ) {
126  std::cout << e.what() << std::endl;
127  }
128  try {
130  } catch ( StandardException &e ) {
131  std::cout << e.what() << std::endl;
132  }
133  try {
135  } catch ( StandardException &e ) {
136  std::cout << e.what() << std::endl;
137  }
138  try {
140  } catch ( StandardException &e ) {
141  std::cout << e.what() << std::endl;
142  }
143  try {
145  } catch ( StandardException &e ) {
146  std::cout << e.what() << std::endl;
147  }
148  try {
150  } catch ( StandardException &e ) {
151  std::cout << e.what() << std::endl;
152  }
153  try {
155  } catch ( StandardException &e ) {
156  std::cout << e.what() << std::endl;
157  }
158  try {
160  } catch ( StandardException &e ) {
161  std::cout << e.what() << std::endl;
162  }
163  try {
165  } catch ( StandardException &e ) {
166  std::cout << e.what() << std::endl;
167  }
168  try {
170  } catch ( StandardException &e ) {
171  std::cout << e.what() << std::endl;
172  }
173  try {
175  } catch ( StandardException &e ) {
176  std::cout << e.what() << std::endl;
177  }
178 
179  std::cout << std::endl;
180  std::cout << "***********************************************************" << std::endl;
181  std::cout << " Standard Exceptions - Custom Message" << std::endl;
182  std::cout << "***********************************************************" << std::endl;
183  std::cout << std::endl;
184 
185  try {
186  throw StandardException(LOC, OutOfRangeError,"Custom detailed message.");
187  } catch ( StandardException &e ) {
188  std::cout << e.what() << std::endl;
189  }
190 
191  std::cout << std::endl;
192  std::cout << "***********************************************************" << std::endl;
193  std::cout << " Standard Exceptions - Custom Message" << std::endl;
194  std::cout << "***********************************************************" << std::endl;
195  std::cout << std::endl;
196  try {
197  throw StandardException(LOC, OutOfRangeError,"Custom detailed message.");
198  } catch ( StandardException &e ) {
199  std::cout << e.what() << std::endl;
200  }
201 
202  std::cout << std::endl;
203  std::cout << "***********************************************************" << std::endl;
204  std::cout << " Assert Exception Macro" << std::endl;
205  std::cout << "***********************************************************" << std::endl;
206  std::cout << std::endl;
207 
208  try {
209  ecl_assert_throw(2<1, StandardException(LOC, InvalidInputError,"Two is not smaller than one!"));
210  } catch ( StandardException &e ) {
211  std::cout << e.what() << std::endl;
212  }
213 
214  std::cout << std::endl;
215  std::cout << "***********************************************************" << std::endl;
216  std::cout << " Debug Throw Macro" << std::endl;
217  std::cout << "***********************************************************" << std::endl;
218  std::cout << std::endl;
219 
220  try {
221  f(7);
222  } catch ( StandardException &e ) {
223  std::cout << e.what() << std::endl;
224  }
225 
226  std::cout << std::endl;
227  std::cout << "***********************************************************" << std::endl;
228  std::cout << " Rethrowing" << std::endl;
229  std::cout << "***********************************************************" << std::endl;
230  std::cout << std::endl;
231 
232  try {
233  throw StandardException(LOC, ConfigurationError,"Starting spot for a rethrow.");
234  } catch ( StandardException &e1 ) {
235  try {
236  throw StandardException(LOC, e1);
237  } catch ( StandardException &e2 ) {
238  std::cout << e2.what() << std::endl;
239  }
240  }
241 
242  std::cout << std::endl;
243  std::cout << "***********************************************************" << std::endl;
244  std::cout << " Try Catch" << std::endl;
245  std::cout << "***********************************************************" << std::endl;
246  std::cout << std::endl;
247 
248 
249  std::cout << "An ecl try-catch block." << std::endl;
250  std::cout << std::endl;
251  StandardException e_original(LOC,UnknownError);
252  ecl_try {
253  std::cout << "Try" << std::endl;
254  ecl_throw(StandardException(LOC,e_original));
255  } ecl_catch( StandardException &e ) {
256  std::cout << "Catch" << std::endl;
257  std::cout << e.what() << std::endl;
258  }
259  std::cout << "An debug try-catch block." << std::endl;
260  std::cout << std::endl;
261  ecl_debug_try {
262  std::cout << "Try" << std::endl;
265  std::cout << "Catch" << std::endl;
266  // std::cout << e.what() << std::endl; // This fails in release
267  }
268 
269 
270  std::cout << std::endl;
271  std::cout << "***********************************************************" << std::endl;
272  std::cout << " Data Exception" << std::endl;
273  std::cout << "***********************************************************" << std::endl;
274  std::cout << std::endl;
275 
276  try {
277  throw DataException<int>(LOC, ConfigurationError,"Throwing a data exception with an int.", 7);
278  } catch ( DataException<int> &e ) {
279  std::cout << "Data: " << e.data() << std::endl;
280  std::cout << e.what() << std::endl;
281  }
282 
283  std::cout << std::endl;
284  std::cout << "***********************************************************" << std::endl;
285  std::cout << " Passed" << std::endl;
286  std::cout << "***********************************************************" << std::endl;
287  std::cout << std::endl;
288 
289  return 0;
290 }
291 
292 #else
293 /*****************************************************************************
294 ** Main
295 *****************************************************************************/
296 
297 int main(int argc, char **argv) {
298  std::cout << "Ecl exceptions are unavailable (ecl was compiled with ECL_DISABLE_EXCEPTIONS)." << std::endl;
299 
300  return 0;
301 }
302 
303 #endif /* ECL_DISABLE_EXCEPTIONS */
ecl::NotInitialisedError
NotInitialisedError
ecl::InvalidInputError
InvalidInputError
ecl::OpenError
OpenError
ecl::ConnectionError
ConnectionError
ecl::WriteError
WriteError
ecl::BusyError
BusyError
ecl::UsageError
UsageError
ecl_try
#define ecl_try
The try part of a try-catch macro matching ecl_throw calls.
Definition: macros.hpp:69
ecl::DataException::what
const char * what() const
Definition: data_exception.hpp:136
ecl::OutOfResourcesError
OutOfResourcesError
main
int main(int argc, char **argv)
Definition: exceptions.cpp:55
ecl::MemoryError
MemoryError
f
void f(int i)
Definition: exceptions.cpp:42
ecl::DestructorError
DestructorError
ecl::ArgNotSupportedError
ArgNotSupportedError
ecl::RaiiError
RaiiError
ecl::ConstructorError
ConstructorError
ecl_debug_try
#define ecl_debug_try
The try part of a try-catch macro matching ecl_debug_throw/ecl_assert_throw calls.
Definition: macros.hpp:120
LOC
#define LOC
ecl::InterruptedError
InterruptedError
ecl::StandardException
Standard exception type, provides code location and error string.
Definition: standard_exception.hpp:62
ecl::NoError
NoError
ecl_debug_catch
#define ecl_debug_catch(exception)
The catch part of a try-catch macro matching ecl_debug_throw/ecl_assert_throw calls.
Definition: macros.hpp:126
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
Debug mode throw with a logical condition check.
Definition: macros.hpp:104
ecl::InvalidArgError
InvalidArgError
ecl::StandardException::what
const char * what() const
Definition: standard_exception.cpp:56
ecl::PermissionsError
PermissionsError
ecl_catch
#define ecl_catch(exception)
The catch part of a try-catch macro matching ecl_throw calls.
Definition: macros.hpp:75
ecl_throw
#define ecl_throw(exception)
Standard ecl throw exception throw.
Definition: macros.hpp:63
ecl::UnknownError
UnknownError
ecl::CloseError
CloseError
ecl::ConversionError
ConversionError
ecl::DataException
Extended exception class that bundles location, message and data.
Definition: data_exception.hpp:66
ecl::NotSupportedError
NotSupportedError
ecl::ReadError
ReadError
ecl::OutOfRangeError
OutOfRangeError
ecl
ecl::ConfigurationError
ConfigurationError
ecl_debug_throw
#define ecl_debug_throw(exception)
Debug mode exception throw.
Definition: macros.hpp:114


ecl_exceptions
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:17