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