Advertisement
Guest User

d_except.h

a guest
Feb 2nd, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #ifndef EXCEPTION_CLASSES
  2. #define EXCEPTION_CLASSES
  3.  
  4. #include <sstream>
  5. #include <string>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. class baseException
  11. {
  12. public:
  13. baseException(const string& str = ""):
  14. msgString(str)
  15. {
  16. if (msgString == "")
  17. msgString = "Unspecified exception";
  18. }
  19.  
  20. string what() const
  21. {
  22. return msgString;
  23. }
  24.  
  25. // protected allows a derived class to access msgString.
  26. // chapter 13 discusses protected in detail
  27. protected:
  28. string msgString;
  29. };
  30.  
  31. // failure to allocate memory (new() returns NULL)
  32. class memoryAllocationError: public baseException
  33. {
  34. public:
  35. memoryAllocationError(const string& msg = ""):
  36. baseException(msg)
  37. {}
  38. };
  39.  
  40. // function argument out of proper range
  41. class rangeError: public baseException
  42. {
  43. public:
  44. rangeError(const string& msg = ""):
  45. baseException(msg)
  46. {}
  47. };
  48.  
  49. // index out of range
  50. class indexRangeError: public baseException
  51. {
  52. public:
  53. indexRangeError(const string& msg, int i, int size):
  54. baseException()
  55. {
  56. ostringstream indexErr;
  57.  
  58. indexErr << msg << " index " << i << " size = " << size << ends;
  59. // indexRangeError can modify msgString, since it is in
  60. // the protected section of baseException
  61. msgString = indexErr.str();
  62. }
  63. };
  64.  
  65. // attempt to erase from an empty container
  66. class underflowError: public baseException
  67. {
  68. public:
  69. underflowError(const string& msg = ""):
  70. baseException(msg)
  71. {}
  72. };
  73.  
  74. // attempt to insert into a full container
  75. class overflowError: public baseException
  76. {
  77. public:
  78. overflowError(const string& msg = ""):
  79. baseException(msg)
  80. {}
  81. };
  82.  
  83. // error in expression evaluation
  84. class expressionError: public baseException
  85. {
  86. public:
  87. expressionError(const string& msg = ""):
  88. baseException(msg)
  89. {}
  90. };
  91.  
  92. // bad object reference
  93. class referenceError: public baseException
  94. {
  95. public:
  96. referenceError(const string& msg = ""):
  97. baseException(msg)
  98. {}
  99. };
  100.  
  101. // feature not implemented
  102. class notImplementedError: public baseException
  103. {
  104. public:
  105. notImplementedError(const string& msg = ""):
  106. baseException(msg)
  107. {}
  108. };
  109.  
  110. // date errors
  111. class dateError: public baseException
  112. {
  113. public:
  114. dateError(const string& first, int v, const string& last):
  115. baseException()
  116. {
  117. ostringstream dateErr;
  118.  
  119. dateErr << first << ' ' << v << ' ' << last << ends;
  120. // dateError can modify msgString, since it is in
  121. // the protected section of baseException
  122. msgString = dateErr.str();
  123. }
  124. };
  125.  
  126. // error in graph class
  127. class graphError: public baseException
  128. {
  129. public:
  130. graphError(const string& msg = ""):
  131. baseException(msg)
  132. {}
  133. };
  134.  
  135. // file open error
  136. class fileOpenError: public baseException
  137. {
  138. public:
  139. fileOpenError(const string& fname):
  140. baseException()
  141. {
  142. ostringstream fileErr;
  143.  
  144. fileErr << "Cannot open \"" << fname << "\"" << ends;
  145. // fileOpenError can modify msgString, since it is in
  146. // the protected section of baseException
  147. msgString = fileErr.str();
  148. }
  149. };
  150.  
  151. // error in graph class
  152. class fileError: public baseException
  153. {
  154. public:
  155. fileError(const string& msg = ""):
  156. baseException(msg)
  157. {}
  158. };
  159.  
  160. #endif // EXCEPTION_CLASSES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement