casacore
Loading...
Searching...
No Matches
Error.h
Go to the documentation of this file.
1//# Error.h: Base class for all Casacore errors
2//# Copyright (C) 1993,1994,1995,1999,2000,2001,2016
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_ERROR_H
27#define CASA_ERROR_H
28
29
30#include <casacore/casa/aips.h>
31#include <casacore/casa/BasicSL/String.h>
32#include <exception>
33#include <sys/types.h>
34
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38// Throw the given exception with a string composed of various arguments.
39// E.g.
40// <srcblock>
41// CASATHROW (AipsError, "integer=" << myint << ", float=" << myfloat);
42// </srcblock>
43#define CASATHROW(exc, arg) do { \
44 std::ostringstream casa_log_oss; \
45 casa_log_oss << arg; \
46 throw exc(casa_log_oss.str()); \
47 } while (0)
48
49// The Assert macro is an alias to the standard assert macro when NDEBUG is defined. When
50// NDEBUG is not defined (release build) then a throw is used to report the error.
51
52#ifdef NDEBUG
53#define AssertCc(c) ((void)0)
54#else
55#define AssertCc(c) { if (AIPS_UNLIKELY(! (c))) {casacore::AipsError::throwIf (casacore::True, "Assertion failed: " #c, __FILE__, __LINE__, __PRETTY_FUNCTION__); }}
56#endif
57
58#define AssertAlways(c) { if (AIPS_UNLIKELY(! (c))) {casacore::AipsError::throwIf (casacore::True, "Assertion failed: " #c, __FILE__, __LINE__, __PRETTY_FUNCTION__); }}
59
60#define WarnCc(m)\
61{\
62 LogIO os(LogOrigin("", __func__, __LINE__, WHERE));\
63 os << LogIO::WARN << m << LogIO::POST;\
64}
65
66
67// Asserts when in debug build and issues a warning message to the log in release.
68#if defined (NDEBUG)
69#define AssertOrWarn(c,m) ((void)0)
70#else
71#define AssertOrWarn(c,m)\
72{ if (AIPS_UNLIKELY(! (c))) {\
73 WarnCc (m);\
74 }\
75}
76#endif
77
78#if defined (NDEBUG)
79# define ThrowCc(m) \
80 { casacore::AipsError anAipsError ((m), __FILE__, __LINE__); \
81 throw anAipsError; }
82#else
83# define ThrowCc(m) throw casacore::AipsError ((m), __FILE__, __LINE__)
84#endif
85
86// Throw an AipsError exception if the condition is true.
87#define ThrowIf(c,m) {if (AIPS_UNLIKELY(c)) {casacore::AipsError::throwIf (casacore::True, (m), __FILE__, __LINE__, __PRETTY_FUNCTION__);}}
88
89// Throw an AipsError exception if the system error code is not 0.
90// It adds the message for that error code to the exception text.
91#define ThrowIfError(c,m) {if (AIPS_UNLIKELY(c)) {casacore::AipsError::throwIfError (casacore::True, (m), __FILE__, __LINE__, __PRETTY_FUNCTION__);}}
92
93// Repackage and rethrow an AipsError exception.
94#define Rethrow(e,m) {throw casacore::AipsError::repackageAipsError ((e),(m),__FILE__,__LINE__, __PRETTY_FUNCTION__);}
95
96
97// <summary>Base class for all Casacore library errors</summary>
98// <use visibility=export>
99//
100// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
101// </reviewed>
102//
103// <prerequisite>
104// <li> ExcpError
105// </prerequisite>
106//
107// <synopsis>
108// This is the base class for all of the Casacore error classes. Because
109// all of the errors have a common base class, any error can be caught
110// with a single catch statement.
111//
112// This class has a string which allows error messages to be propagated.
113//
114// <note role=tip> The string member must be handled very carefully because
115// string is also derived from cleanup, thus the
116// <src>message.makePermanent()</src> call in the implementation of
117// the constructors. This prevents the String from being cleaned up
118// in the middle of an exception.
119// </note>
120//
121// </synopsis>
122//
123// <example>
124// <srcblock>
125// throw(AipsError("SOME STRING"));
126// </srcblock>
127// </example>
128//
129// <todo asof="">
130// </todo>
131
132class AipsError: public std::exception
133{
134public:
135
140
141 //
142 // Simply returns the stored error message.
143 //
144 virtual const char* what() const noexcept
145 { return(message.c_str()); }
146 const String &getMesg() const
147 { return(message); }
150 { return(category); }
151
152 // Append a message. This is used by LogIO when an exception is logged.
153 // The message is const to be able to use it for a temporary exception.
154 void setMessage (const String& msg) const
155 { const_cast<AipsError*>(this)->message = msg; }
156
157 // Creates an AipsError and initializes the error message from
158 // the parameter.
159 // <group>
160 AipsError (const Char *str, Category c = GENERAL);
161 AipsError (const String &str, Category c = GENERAL);
162 AipsError (const String &msg, const String &filename, uInt lineNumber,
163 Category c = GENERAL);
165 // </group>
166
167 //
168 // Destructor which does nothing.
169 //
170 ~AipsError() noexcept;
171
172 // Get or clear the stacktrace info.
173 // <group>
177 static void clearLastInfo ();
178 // </group>
179
180 // Repackage an exception.
182 const String& message,
183 const char* file,
184 Int line,
185 const char* func);
186
187 // Throw if the condition is true.
188 static void throwIf (Bool condition, const String& message,
189 const char* file, Int line,
190 const char* func = "");
191
192 // Throw if the system error code is not 0.
193 static void throwIfError (Int errorCode, const String& prefix,
194 const char* file, Int line,
195 const char* func = "");
196
197protected:
198 // Add the stack trace to the message (if USE_STACKTRACE is set).
200
204};
205
206
207// <summary>Allocation errors</summary>
208// <use visibility=export>
209//
210// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
211// </reviewed>
212//
213// <synopsis>
214//
215// This class is used for allocation errors. It adds an extra
216// data item, the failed allocation size. Otherwise much the
217// same as <src>AipsError</src>.
218//
219// </synopsis>
220//
221// <example>
222// <srcblock>
223// throw(AllocError("ANY STRING",1024));
224// </srcblock>
225// </example>
226//
227// <todo asof="">
228// </todo>
229
230class AllocError : public AipsError {
231protected:
232 size_t Size;
233public:
234 //
235 // This constructor takes the error message and the failed
236 // allocation size.
237 //
238 // <group>
239 AllocError(const Char *str, uInt sze) : AipsError(str,SYSTEM), Size(sze) {}
240 AllocError(const String &str, uInt sze) : AipsError(str,SYSTEM), Size(sze) {}
241 // </group>
242
243 //
244 // This function returns the failed allocation size.
245 //
246 size_t size() const {return(Size);}
247
248 //
249 // Destructor which does nothing.
250 //
251 ~AllocError() noexcept;
252
253};
254
255
256// <summary>Base class for all indexing errors</summary>
257// <use visibility=export>
258//
259// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
260// </reviewed>
261//
262// <synopsis>
263// This class is the base class of all <src>IndexError</src>s. It is
264// defined to allow the user to catch any of the many kinds of IndexErrors
265// which may be thrown. It can also be thrown itself if returning
266// the illegal index value is unimportant.
267// </synopsis>
268//
269// <example>
270// <srcblock>
271// throw(IndexError("ANY STRING"));
272// </srcblock>
273// </example>
274//
275// <todo asof="">
276// </todo>
277
278class IndexError : public AipsError {
279public:
280 //
281 // Creates an GeneralIndexError and initializes the error message from
282 // the parameter
283 // <group>
284 IndexError(const Char *str,Category c=BOUNDARY) : AipsError(str,c) {}
285 IndexError(const String &str,Category c=BOUNDARY) : AipsError(str,c) {}
287 // </group>
288
289 //
290 // Destructor which does nothing.
291 //
292 ~IndexError() noexcept;
293};
294
295
296// <summary>Index errors returning the bad index</summary>
297// <use visibility=export>
298//
299// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
300// </reviewed>
301//
302// <synopsis>
303// This class is templated to allow generalalized indexes to be returned
304// with the error message i.e. the class is templated on the index type.
305//
306// </synopsis>
307//
308// <example>
309// <srcblock>
310// throw(indexError<int>(3,"ANY STRING"));/
311// </srcblock>
312// </example>
313//
314// <todo asof="">
315// </todo>
316
317template<class t> class indexError : public IndexError {
318protected:
319 t oIndex; // Offending Index
320public:
321 //
322 // This constructor takes the error message and the index
323 // which cause the error to occur.
324 //
325 // <group>
326 indexError(t oI, const Char *str, Category c=BOUNDARY);
327 indexError(t oI, const String &str, Category c=BOUNDARY);
328 indexError(t oI, Category c=BOUNDARY) : IndexError(c), oIndex(oI) {};
329 // </group>
330
331 //
332 // Destructor which does nothing.
333 //
334 ~indexError() noexcept;
335};
336
337
338// <summary>Duplicate key errors</summary>
339// <use visibility=export>
340//
341// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
342// </reviewed>
343//
344// <synopsis>
345// This class is the base class of all duplicate key errors. It is
346// defined to allow the user to catch any of the many kinds of DuplErrors
347// which may be thrown. It can also be thrown itself if returning
348// the illegal key is unimportant.
349// </synopsis>
350//
351// <example>
352// <srcblock>
353// throw(DuplError("ANY STRING"));
354// </srcblock>
355// </example>
356//
357// <todo asof="">
358// </todo>
359
360class DuplError : public AipsError {
361public:
362 //
363 // Creates an DuplError and initializes the error message from
364 // the parameter
365 // <group>
367 DuplError(const Char *str,Category c=BOUNDARY) : AipsError(str,c) {}
368 DuplError(const String &str,Category c=BOUNDARY) : AipsError(str,c) {}
369 // </group>
370
371 //
372 // Destructor which does nothing.
373 //
374 ~DuplError() noexcept;
375};
376
377
378// <summary>Duplicate key errors where the bad key is returned</summary>
379// <use visibility=export>
380//
381// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
382// </reviewed>
383//
384// <synopsis>
385// This template is for generalized duplicate key errors where the template
386// type parameter is the type of the key which caused the error. Because this
387// class is derived from <linkto class=DuplError><src>DuplError</src>
388// </linkto>, the user to catch all duplicate key errors with one catch
389// statement.
390//
391// </synopsis>
392//
393// <example>
394// throw(duplError<int>(4,"ANY STRING"));
395// </example>
396//
397// <todo asof="">
398// </todo>
399
400template<class t> class duplError : public DuplError {
401protected:
402 t oKey; // Offending Key
403public:
404 //
405 // This constructs a "duplError" for the offending key, and an
406 // optional character string.
407 //
408 // <group>
409 duplError(t oI, const Char *str,Category c=BOUNDARY);
410 duplError(t oI, const String &str,Category c=BOUNDARY);
411 duplError(t oI,Category c=BOUNDARY) : DuplError(c), oKey(oI) {};
412 // </group>
413
414 //
415 // Destructor which does nothing.
416 //
417 ~duplError() noexcept;
418};
419
420
421// <summary>Exception for an error in a system call</summary>
422// <use visibility=export>
423//
424// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
425// </reviewed>
426//
427// <synopsis>
428// This error is to be used for if a system call returns an error.
429// It uses strerror to get the system error message.
430// </synopsis>
431
433{
434public:
435 // This constructs a "SystemCallError" from the system call function name
436 // and the errno.
437 SystemCallError(const String &funcName, int error, Category c=GENERAL);
438
439 SystemCallError (int error, const String &msg, const String &filename,
440 uInt lineNumber, Category c=GENERAL);
441
442 // Destructor which does nothing.
444
445 // Get the errno.
446 int error() const
447 { return itsError; }
448
449 // Get the message belonging to an error.
450 static String errorMessage(int error);
451
452private:
454};
455
456
457// <summary>Exception which halts execution</summary>
458// <use visibility=export>
459//
460// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
461// </reviewed>
462//
463// <synopsis>
464// This error causes an execution to halt regardless. It
465// causes execution to halt before the exception can be caught.
466// </synopsis>
467//
468// <example>
469// <srcblock>
470// throw(AbortError("ANY STRING"));
471// </srcblock>
472// </example>
473//
474// <todo asof="">
475// </todo>
476
477class AbortError : public AipsError {
478public:
479 //
480 // This constructs a "AbortError" from the error message.
481 //
482 // <group>
483 AbortError(const Char *str,Category c=GENERAL);
484 AbortError(const String &str,Category c=GENERAL);
485 // </group>
486
487 //
488 // Destructor which does nothing.
489 //
490 ~AbortError() noexcept;
491};
492
493
494// <summary>Initialization error, typically of static data shared between objects</summary>
495// <use visibility=export>
496//
497// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
498// </reviewed>
499//
500// <synopsis>
501// This error indicates that some initialization has failed. It is preferable
502// to throw this in an initX() function called by std::call_once() or similar
503// over returning a bool or other result variable.
504// </synopsis>
505//
506// <todo asof="">
507// </todo>
508
509class InitError : public AipsError {
510};
511
512} //# NAMESPACE CASACORE - END
513
514#ifdef AIPS_NEEDS_RETHROW
515#ifndef CASACORE_NEEDS_RETHROW
516#define CASACORE_NEEDS_RETHROW
517#endif
518#endif
519
520#ifdef CASACORE_NEEDS_RETHROW
521#define RETHROW(X) throw(X);
522#else
523#define RETHROW(X)
524#endif
525
526#ifndef CASACORE_NO_AUTO_TEMPLATES
527#include <casacore/casa/Exceptions/Error.tcc>
528#endif //# CASACORE_NO_AUTO_TEMPLATES
529#endif
Exception which halts execution.
Definition Error.h:477
AbortError(const Char *str, Category c=GENERAL)
This constructs a "AbortError" from the error message.
AbortError(const String &str, Category c=GENERAL)
~AbortError() noexcept
Destructor which does nothing.
~AipsError() noexcept
Destructor which does nothing.
static String getLastStackTrace()
AipsError::Category getCategory() const
Definition Error.h:149
String getStackTrace() const
static void throwIfError(Int errorCode, const String &prefix, const char *file, Int line, const char *func="")
Throw if the system error code is not 0.
AipsError(Category c=GENERAL)
AipsError(const Char *str, Category c=GENERAL)
Creates an AipsError and initializes the error message from the parameter.
static void getLastInfo(String &message, String &stackTrace)
Get or clear the stacktrace info.
static AipsError repackageAipsError(AipsError &error, const String &message, const char *file, Int line, const char *func)
Repackage an exception.
const String & getMesg() const
Definition Error.h:146
static void throwIf(Bool condition, const String &message, const char *file, Int line, const char *func="")
Throw if the condition is true.
static String getLastMessage()
void setMessage(const String &msg) const
Append a message.
Definition Error.h:154
static void clearLastInfo()
virtual const char * what() const noexcept
Simply returns the stored error message.
Definition Error.h:144
AipsError(const String &str, Category c=GENERAL)
void addStackTrace()
Add the stack trace to the message (if USE_STACKTRACE is set).
AipsError(const String &msg, const String &filename, uInt lineNumber, Category c=GENERAL)
Category category
Definition Error.h:202
String stackTrace
Definition Error.h:203
Allocation errors.
Definition Error.h:230
size_t size() const
This function returns the failed allocation size.
Definition Error.h:246
AllocError(const Char *str, uInt sze)
This constructor takes the error message and the failed allocation size.
Definition Error.h:239
~AllocError() noexcept
Destructor which does nothing.
AllocError(const String &str, uInt sze)
Definition Error.h:240
Duplicate key errors.
Definition Error.h:360
DuplError(const String &str, Category c=BOUNDARY)
Definition Error.h:368
DuplError(Category c=BOUNDARY)
Creates an DuplError and initializes the error message from the parameter.
Definition Error.h:366
DuplError(const Char *str, Category c=BOUNDARY)
Definition Error.h:367
~DuplError() noexcept
Destructor which does nothing.
Base class for all indexing errors.
Definition Error.h:278
~IndexError() noexcept
Destructor which does nothing.
IndexError(const Char *str, Category c=BOUNDARY)
Creates an GeneralIndexError and initializes the error message from the parameter.
Definition Error.h:284
IndexError(const String &str, Category c=BOUNDARY)
Definition Error.h:285
IndexError(Category c=BOUNDARY)
Definition Error.h:286
Initialization error, typically of static data shared between objects.
Definition Error.h:509
String: the storage and methods of handling collections of characters.
Definition String.h:223
const Char * c_str() const
Get char array.
Definition String.h:555
Exception for an error in a system call.
Definition Error.h:433
static String errorMessage(int error)
Get the message belonging to an error.
~SystemCallError() noexcept
Destructor which does nothing.
SystemCallError(const String &funcName, int error, Category c=GENERAL)
This constructs a "SystemCallError" from the system call function name and the errno.
SystemCallError(int error, const String &msg, const String &filename, uInt lineNumber, Category c=GENERAL)
Duplicate key errors where the bad key is returned.
Definition Error.h:400
duplError(t oI, Category c=BOUNDARY)
Definition Error.h:411
duplError(t oI, const String &str, Category c=BOUNDARY)
duplError(t oI, const Char *str, Category c=BOUNDARY)
This constructs a "duplError" for the offending key, and an optional character string.
~duplError() noexcept
Destructor which does nothing.
Index errors returning the bad index.
Definition Error.h:317
indexError(t oI, Category c=BOUNDARY)
Definition Error.h:328
~indexError() noexcept
Destructor which does nothing.
indexError(t oI, const String &str, Category c=BOUNDARY)
indexError(t oI, const Char *str, Category c=BOUNDARY)
This constructor takes the error message and the index which cause the error to occur.
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
char Char
Definition aipstype.h:44