PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/casa/Exceptions/Error.h

http://casacore.googlecode.com/
C Header | 435 lines | 110 code | 55 blank | 270 comment | 1 complexity | dc9d02edcc4c6add5950d2cbef2f9579 MD5 | raw file
Possible License(s): GPL-2.0
  1. //# Error.h: Base class for all AIPS++ errors
  2. //# Copyright (C) 1993,1994,1995,1999,2000,2001
  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: aips2-request@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. //# $Id: Error.h 21051 2011-04-20 11:46:29Z gervandiepen $
  27. #ifndef CASA_ERROR_H
  28. #define CASA_ERROR_H
  29. #include <sys/types.h>
  30. #include <casa/aips.h>
  31. #include <casa/BasicSL/String.h>
  32. #include <exception>
  33. namespace casa { //# NAMESPACE CASA - BEGIN
  34. // Throw an exception with a string composed of various arguments.
  35. // E.g.
  36. // <srcblock>
  37. // CASATHROW (AipsError, "integer=" << myint << ", float=" << myfloat)
  38. // </srcblock>
  39. #define CASATHROW(exc, arg) do { \
  40. std::ostringstream casa_log_oss; \
  41. casa_log_oss << arg; \
  42. throw exc(casa_log_oss.str()); \
  43. } while (0)
  44. // <summary>Base class for all AIPS++ library errors</summary>
  45. // <use visibility=export>
  46. //
  47. // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
  48. // </reviewed>
  49. //
  50. // <prerequisite>
  51. // <li> ExcpError
  52. // </prerequisite>
  53. //
  54. // <synopsis>
  55. // This is the base class for all of the AIPS++ error classes. Because
  56. // all of the errors have a common base class, any error can be caught
  57. // with a single catch statement.
  58. //
  59. // This class has a string which allows error messages to be propagated.
  60. //
  61. // <note role=tip> The string member must be handled very carefully because
  62. // string is also derived from cleanup, thus the
  63. // <src>message.makePermanent()</src> call in the implementation of
  64. // the constructors. This prevents the String from being cleaned up
  65. // in the middle of an exception.
  66. // </note>
  67. //
  68. // </synopsis>
  69. //
  70. // <example>
  71. // <srcblock>
  72. // throw(AipsError("SOME STRING"));
  73. // </srcblock>
  74. // </example>
  75. //
  76. // <todo asof="">
  77. // </todo>
  78. class AipsError: public std::exception
  79. {
  80. public:
  81. enum Category {
  82. BOUNDARY, INITIALIZATION, INVALID_ARGUMENT, CONFORMANCE,
  83. ENVIRONMENT, SYSTEM, PERMISSION, GENERAL
  84. };
  85. //
  86. // Simply returns the stored error message.
  87. //
  88. virtual const char* what() const throw()
  89. { return(message.c_str()); }
  90. const String &getMesg() const
  91. { return(message); }
  92. AipsError::Category getCategory( ) const
  93. { return(category); }
  94. // Append a message. This is used by LogIO when an exception is logged.
  95. // The message is const to be able to use it for a temporary exception.
  96. void setMessage (const String& msg) const
  97. { const_cast<AipsError*>(this)->message = msg; }
  98. // Creates an AipsError and initializes the error message from
  99. // the parameter.
  100. // <group>
  101. AipsError (const Char *str, Category c = GENERAL);
  102. AipsError (const String &str, Category c = GENERAL);
  103. AipsError (const String &msg, const String &filename, uInt lineNumber,
  104. Category c = GENERAL);
  105. AipsError (Category c = GENERAL) : message(), category(c) {};
  106. // </group>
  107. //
  108. // Destructor which does nothing.
  109. //
  110. ~AipsError() throw();
  111. protected:
  112. String message;
  113. Category category;
  114. };
  115. // <summary>Allocation errors</summary>
  116. // <use visibility=export>
  117. //
  118. // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
  119. // </reviewed>
  120. //
  121. // <synopsis>
  122. //
  123. // This class is used for allocation errors. It adds an extra
  124. // data item, the failed allocation size. Otherwise much the
  125. // same as <src>AipsError</src>.
  126. //
  127. // </synopsis>
  128. //
  129. // <example>
  130. // <srcblock>
  131. // throw(AllocError("ANY STRING",1024));
  132. // </srcblock>
  133. // </example>
  134. //
  135. // <todo asof="">
  136. // </todo>
  137. class AllocError : public AipsError {
  138. protected:
  139. size_t Size;
  140. public:
  141. //
  142. // This constructor takes the error message and the failed
  143. // allocation size.
  144. //
  145. // <group>
  146. AllocError(const Char *str, uInt sze) : AipsError(str,SYSTEM), Size(sze) {}
  147. AllocError(const String &str, uInt sze) : AipsError(str,SYSTEM), Size(sze) {}
  148. // </group>
  149. //
  150. // This function returns the failed allocation size.
  151. //
  152. size_t size() const {return(Size);}
  153. //
  154. // Destructor which does nothing.
  155. //
  156. ~AllocError() throw();
  157. };
  158. // <summary>Base class for all indexing errors</summary>
  159. // <use visibility=export>
  160. //
  161. // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
  162. // </reviewed>
  163. //
  164. // <synopsis>
  165. // This class is the base class of all <src>IndexError</src>s. It is
  166. // defined to allow the user to catch any of the many kinds of IndexErrors
  167. // which may be thrown. It can also be thrown itself if returning
  168. // the illegal index value is unimportant.
  169. // </synopsis>
  170. //
  171. // <example>
  172. // <srcblock>
  173. // throw(IndexError("ANY STRING"));
  174. // </srcblock>
  175. // </example>
  176. //
  177. // <todo asof="">
  178. // </todo>
  179. class IndexError : public AipsError {
  180. public:
  181. //
  182. // Creates an GeneralIndexError and initializes the error message from
  183. // the parameter
  184. // <group>
  185. IndexError(const Char *str,Category c=BOUNDARY) : AipsError(str,c) {}
  186. IndexError(const String &str,Category c=BOUNDARY) : AipsError(str,c) {}
  187. IndexError(Category c=BOUNDARY) : AipsError(c) {}
  188. // </group>
  189. //
  190. // Destructor which does nothing.
  191. //
  192. ~IndexError() throw();
  193. };
  194. // <summary>Index errors returning the bad index</summary>
  195. // <use visibility=export>
  196. //
  197. // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
  198. // </reviewed>
  199. //
  200. // <synopsis>
  201. // This class is templated to allow generalalized indexes to be returned
  202. // with the error message i.e. the class is templated on the index type.
  203. //
  204. // </synopsis>
  205. //
  206. // <example>
  207. // <srcblock>
  208. // throw(indexError<int>(3,"ANY STRING"));/
  209. // </srcblock>
  210. // </example>
  211. //
  212. // <todo asof="">
  213. // </todo>
  214. template<class t> class indexError : public IndexError {
  215. protected:
  216. t oIndex; // Offending Index
  217. public:
  218. //
  219. // This constructor takes the error message and the index
  220. // which cause the error to occur.
  221. //
  222. // <group>
  223. indexError(t oI, const Char *str, Category c=BOUNDARY);
  224. indexError(t oI, const String &str, Category c=BOUNDARY);
  225. indexError(t oI, Category c=BOUNDARY) : IndexError(c), oIndex(oI) {};
  226. // </group>
  227. //
  228. // Destructor which does nothing.
  229. //
  230. ~indexError() throw();
  231. };
  232. // <summary>Duplicate key errors</summary>
  233. // <use visibility=export>
  234. //
  235. // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
  236. // </reviewed>
  237. //
  238. // <synopsis>
  239. // This class is the base class of all duplicate key errors. It is
  240. // defined to allow the user to catch any of the many kinds of DuplErrors
  241. // which may be thrown. It can also be thrown itself if returning
  242. // the illegal key is unimportant.
  243. // </synopsis>
  244. //
  245. // <example>
  246. // <srcblock>
  247. // throw(DuplError("ANY STRING"));
  248. // </srcblock>
  249. // </example>
  250. //
  251. // <todo asof="">
  252. // </todo>
  253. class DuplError : public AipsError {
  254. public:
  255. //
  256. // Creates an DuplError and initializes the error message from
  257. // the parameter
  258. // <group>
  259. DuplError(Category c=BOUNDARY) : AipsError(c) {}
  260. DuplError(const Char *str,Category c=BOUNDARY) : AipsError(str,c) {}
  261. DuplError(const String &str,Category c=BOUNDARY) : AipsError(str,c) {}
  262. // </group>
  263. //
  264. // Destructor which does nothing.
  265. //
  266. ~DuplError() throw();
  267. };
  268. // <summary>Duplicate key errors where the bad key is returned</summary>
  269. // <use visibility=export>
  270. //
  271. // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
  272. // </reviewed>
  273. //
  274. // <synopsis>
  275. // This template is for generalized duplicate key errors where the template
  276. // type parameter is the type of the key which caused the error. Because this
  277. // class is derived from <linkto class=DuplError><src>DuplError</src>
  278. // </linkto>, the user to catch all duplicate key errors with one catch
  279. // statement.
  280. //
  281. // </synopsis>
  282. //
  283. // <example>
  284. // throw(duplError<int>(4,"ANY STRING"));
  285. // </example>
  286. //
  287. // <todo asof="">
  288. // </todo>
  289. template<class t> class duplError : public DuplError {
  290. protected:
  291. t oKey; // Offending Key
  292. public:
  293. //
  294. // This constructs a "duplError" for the offending key, and an
  295. // optional character string.
  296. //
  297. // <group>
  298. duplError(t oI, const Char *str,Category c=BOUNDARY);
  299. duplError(t oI, const String &str,Category c=BOUNDARY);
  300. duplError(t oI,Category c=BOUNDARY) : DuplError(c), oKey(oI) {};
  301. // </group>
  302. //
  303. // Destructor which does nothing.
  304. //
  305. ~duplError() throw();
  306. };
  307. // <summary>Exception for an error in a system call</summary>
  308. // <use visibility=export>
  309. //
  310. // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
  311. // </reviewed>
  312. //
  313. // <synopsis>
  314. // This error is to be used for if a system call returns an error.
  315. // It uses strerror to get the system error message.
  316. // </synopsis>
  317. class SystemCallError : public AipsError
  318. {
  319. public:
  320. // This constructs a "SystemCallError" from the system call function name
  321. // and the errno.
  322. SystemCallError(const String &funcName, int error, Category c=GENERAL);
  323. // Destructor which does nothing.
  324. ~SystemCallError() throw();
  325. // Get the errno.
  326. int error() const
  327. { return itsError; }
  328. // Get the message belonging to an error.
  329. static String errorMessage(int error);
  330. private:
  331. int itsError;
  332. };
  333. // <summary>Exception which halts execution</summary>
  334. // <use visibility=export>
  335. //
  336. // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
  337. // </reviewed>
  338. //
  339. // <synopsis>
  340. // This error causes an execution to halt regardless. It
  341. // causes execution to halt before the exception can be caught.
  342. // </synopsis>
  343. //
  344. // <example>
  345. // <srcblock>
  346. // throw(AbortError("ANY STRING"));
  347. // </srcblock>
  348. // </example>
  349. //
  350. // <todo asof="">
  351. // </todo>
  352. class AbortError : public AipsError {
  353. public:
  354. //
  355. // This constructs a "AbortError" from the error message.
  356. //
  357. // <group>
  358. AbortError(const Char *str,Category c=GENERAL);
  359. AbortError(const String &str,Category c=GENERAL);
  360. // </group>
  361. //
  362. // Destructor which does nothing.
  363. //
  364. ~AbortError() throw();
  365. };
  366. } //# NAMESPACE CASA - END
  367. #ifdef AIPS_NEEDS_RETHROW
  368. #ifndef CASACORE_NEEDS_RETHROW
  369. #define CASACORE_NEEDS_RETHROW
  370. #endif
  371. #endif
  372. #ifdef CASACORE_NEEDS_RETHROW
  373. #define RETHROW(X) throw(X);
  374. #else
  375. #define RETHROW(X)
  376. #endif
  377. #ifndef CASACORE_NO_AUTO_TEMPLATES
  378. #include <casa/Exceptions/Error.tcc>
  379. #endif //# CASACORE_NO_AUTO_TEMPLATES
  380. #endif