IexBaseExc.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IEXBASEEXC_H
  35. #define INCLUDED_IEXBASEEXC_H
  36. #include "IexNamespace.h"
  37. #include "IexExport.h"
  38. //----------------------------------------------------------
  39. //
  40. // A general exception base class, and a few
  41. // useful exceptions derived from the base class.
  42. //
  43. //----------------------------------------------------------
  44. #include <string>
  45. #include <exception>
  46. #include <sstream>
  47. IEX_INTERNAL_NAMESPACE_HEADER_ENTER
  48. //-------------------------------
  49. // Our most basic exception class
  50. //-------------------------------
  51. class BaseExc: public std::exception
  52. {
  53. public:
  54. //----------------------------
  55. // Constructors and destructor
  56. //----------------------------
  57. IEX_EXPORT BaseExc (const char *s = 0) throw(); // std::string (s)
  58. IEX_EXPORT BaseExc (const std::string &s) throw(); // std::string (s)
  59. IEX_EXPORT BaseExc (std::stringstream &s) throw(); // std::string (s.str())
  60. IEX_EXPORT BaseExc (const BaseExc &be) throw();
  61. IEX_EXPORT virtual ~BaseExc () throw ();
  62. //---------------------------------------------------
  63. // what() method -- e.what() returns _message.c_str()
  64. //---------------------------------------------------
  65. IEX_EXPORT virtual const char * what () const throw ();
  66. //--------------------------------------------------
  67. // Convenient methods to change the exception's text
  68. //--------------------------------------------------
  69. IEX_EXPORT BaseExc & assign (std::stringstream &s); // assign (s.str())
  70. IEX_EXPORT BaseExc & operator = (std::stringstream &s);
  71. IEX_EXPORT BaseExc & append (std::stringstream &s); // append (s.str())
  72. IEX_EXPORT BaseExc & operator += (std::stringstream &s);
  73. //--------------------------------------------------
  74. // These methods from the base class get obscured by
  75. // the definitions above.
  76. //--------------------------------------------------
  77. IEX_EXPORT BaseExc & assign (const char *s);
  78. IEX_EXPORT BaseExc & operator = (const char *s);
  79. IEX_EXPORT BaseExc & append (const char *s);
  80. IEX_EXPORT BaseExc & operator += (const char *s);
  81. //---------------------------------------------------
  82. // Access to the string representation of the message
  83. //---------------------------------------------------
  84. IEX_EXPORT const std::string & message () const;
  85. //--------------------------------------------------
  86. // Stack trace for the point at which the exception
  87. // was thrown. The stack trace will be an empty
  88. // string unless a working stack-tracing routine
  89. // has been installed (see below, setStackTracer()).
  90. //--------------------------------------------------
  91. IEX_EXPORT const std::string & stackTrace () const;
  92. private:
  93. std::string _message;
  94. std::string _stackTrace;
  95. };
  96. //-----------------------------------------------------
  97. // A macro to save typing when declararing an exception
  98. // class derived directly or indirectly from BaseExc:
  99. //-----------------------------------------------------
  100. #define DEFINE_EXC_EXP(exp, name, base) \
  101. class name: public base \
  102. { \
  103. public: \
  104. exp name() throw(): base (0) {} \
  105. exp name (const char* text) throw(): base (text) {} \
  106. exp name (const std::string &text) throw(): base (text) {} \
  107. exp name (std::stringstream &text) throw(): base (text) {} \
  108. exp ~name() throw() { } \
  109. };
  110. // For backward compatibility.
  111. #define DEFINE_EXC(name, base) DEFINE_EXC_EXP(, name, base)
  112. //--------------------------------------------------------
  113. // Some exceptions which should be useful in most programs
  114. //--------------------------------------------------------
  115. DEFINE_EXC_EXP (IEX_EXPORT, ArgExc, BaseExc) // Invalid arguments to a function call
  116. DEFINE_EXC_EXP (IEX_EXPORT, LogicExc, BaseExc) // General error in a program's logic,
  117. // for example, a function was called
  118. // in a context where the call does
  119. // not make sense.
  120. DEFINE_EXC_EXP (IEX_EXPORT, InputExc, BaseExc) // Invalid input data, e.g. from a file
  121. DEFINE_EXC_EXP (IEX_EXPORT, IoExc, BaseExc) // Input or output operation failed
  122. DEFINE_EXC_EXP (IEX_EXPORT, MathExc, BaseExc) // Arithmetic exception; more specific
  123. // exceptions derived from this class
  124. // are defined in ExcMath.h
  125. DEFINE_EXC_EXP (IEX_EXPORT, ErrnoExc, BaseExc) // Base class for exceptions corresponding
  126. // to errno values (see errno.h); more
  127. // specific exceptions derived from this
  128. // class are defined in ExcErrno.h
  129. DEFINE_EXC_EXP (IEX_EXPORT, NoImplExc, BaseExc) // Missing method exception e.g. from a
  130. // call to a method that is only partially
  131. // or not at all implemented. A reminder
  132. // to lazy software people to get back
  133. // to work.
  134. DEFINE_EXC_EXP (IEX_EXPORT, NullExc, BaseExc) // A pointer is inappropriately null.
  135. DEFINE_EXC_EXP (IEX_EXPORT, TypeExc, BaseExc) // An object is an inappropriate type,
  136. // i.e. a dynamnic_cast failed.
  137. //----------------------------------------------------------------------
  138. // Stack-tracing support:
  139. //
  140. // setStackTracer(st)
  141. //
  142. // installs a stack-tracing routine, st, which will be called from
  143. // class BaseExc's constructor every time an exception derived from
  144. // BaseExc is thrown. The stack-tracing routine should return a
  145. // string that contains a printable representation of the program's
  146. // current call stack. This string will be stored in the BaseExc
  147. // object; the string is accesible via the BaseExc::stackTrace()
  148. // method.
  149. //
  150. // setStackTracer(0)
  151. //
  152. // removes the current stack tracing routine. When an exception
  153. // derived from BaseExc is thrown, the stack trace string stored
  154. // in the BaseExc object will be empty.
  155. //
  156. // stackTracer()
  157. //
  158. // returns a pointer to the current stack-tracing routine, or 0
  159. // if there is no current stack stack-tracing routine.
  160. //
  161. //----------------------------------------------------------------------
  162. typedef std::string (* StackTracer) ();
  163. IEX_EXPORT void setStackTracer (StackTracer stackTracer);
  164. IEX_EXPORT StackTracer stackTracer ();
  165. IEX_INTERNAL_NAMESPACE_HEADER_EXIT
  166. #endif // INCLUDED_IEXBASEEXC_H