OpenMPCD
Exceptions.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines exception classes.
4  */
5 
6 #ifndef OPENMPCD_EXCEPTIONS_HPP
7 #define OPENMPCD_EXCEPTIONS_HPP
8 
9 #include <cstdio>
10 #include <string>
11 
12 /**
13  * Throws the given `ExceptionType`, passing the given `message` along with
14  * file and line number information to the exception's argument as an instance
15  * of `std::string`.
16  *
17  * @param[in] ExceptionType
18  * The type to throw.
19  * @param[in] message
20  * The message to append to file and line number information.
21  */
22 #define OPENMPCD_THROW(ExceptionType, message) do{const char* const _file=__FILE__; const int _line=__LINE__; \
23  char _lineString[10]; \
24  sprintf(_lineString, "%d", _line); \
25  std::string t(message); \
26  t+="\r\n\r\nFile: "; \
27  t+=_file; t+="\r\nLine: "; \
28  t+=_lineString; \
29  throw(ExceptionType(t)); \
30  } while(0)
31 
32 namespace OpenMPCD
33 {
34  /**
35  * The base exception class for OpenMPCD.
36  */
37  class Exception
38  {
39  public:
40  /**
41  * The constructor.
42  * @param[in] msg The exception message.
43  */
44  Exception(const std::string& msg) : message(msg)
45  {
46  }
47 
48  public:
49  /**
50  * Returns the exception message.
51  */
52  const std::string& getMessage() const
53  {
54  return message;
55  }
56 
57  private:
58  std::string message; ///< The exception message.
59  };
60 
61  /**
62  * Represents an exception that is due to an assertion violation.
63  */
65  {
66  public:
67  /**
68  * The constructor.
69  * @param[in] assertion The assertion that has been violated.
70  */
71  AssertionException(const std::string& assertion)
72  : Exception("Assertion violated:\n" + assertion)
73  {
74  }
75  };
76 
77  /**
78  * Represents an invalid configuration.
79  */
81  {
82  public:
83  /**
84  * The constructor.
85  * @param[in] setting The setting that caused the raising of the exception.
86  */
87  InvalidConfigurationException(const std::string& setting)
88  : Exception("Inavlid configuration setting: "+setting)
89  {
90  }
91  };
92 
93  /**
94  * NULL-pointer exception
95  */
97  {
98  public:
99  /**
100  * The constructor.
101  * @param[in] variable The variable name.
102  */
103  NULLPointerException(const std::string& variable)
104  : Exception("NULL pointer given: " + variable)
105  {
106  }
107  };
108 
109  /**
110  * Exception for out-of-bounds access.
111  */
113  {
114  public:
115  /**
116  * The constructor.
117  * @param[in] msg The exception message.
118  */
119  OutOfBoundsException(const std::string& msg)
120  : Exception("Out of bounds access: "+msg)
121  {
122  }
123  };
124 
125  /**
126  * Invalid argument exception.
127  */
129  {
130  public:
131  /**
132  * The constructor.
133  * @param[in] msg The exception message.
134  */
135  InvalidArgumentException(const std::string& msg)
136  : Exception("Invalid argument: "+msg)
137  {
138  }
139  };
140 
141  /**
142  * Exception for a forbidden function call.
143  */
145  {
146  public:
147  /**
148  * The constructor.
149  * @param[in] msg The exception message.
150  */
151  InvalidCallException(const std::string& msg)
152  : Exception(msg)
153  {
154  }
155  };
156 
157  /**
158  * Error on IO.
159  */
160  class IOException : public Exception
161  {
162  public:
163  /**
164  * The constructor.
165  * @param[in] msg The exception message.
166  */
167  IOException(const std::string& msg)
168  : Exception(msg)
169  {
170  }
171  };
172 
173  /**
174  * Division by zero.
175  */
177  {
178  public:
179  /**
180  * The constructor.
181  * @param[in] msg The exception message.
182  */
183  DivisionByZeroException(const std::string& msg)
184  : Exception(msg)
185  {
186  }
187  };
188 
189  /**
190  * Exception for unimplemented functionality.
191  */
193  {
194  public:
195  /**
196  * The constructor.
197  * @param[in] msg The exception message.
198  */
199  UnimplementedException(const std::string& msg)
200  : Exception(msg)
201  {
202  }
203  };
204 
205  /**
206  * Exception for errors in memory management.
207  */
209  {
210  public:
211  /**
212  * The constructor.
213  * @param[in] msg The exception message.
214  */
215  MemoryManagementException(const std::string& msg)
216  : Exception(msg)
217  {
218  }
219  };
220 
221  /**
222  * Represents an exception that signals a malformed file.
223  */
225  {
226  public:
227  /**
228  * The constructor.
229  * @param[in] msg The exception message.
230  */
231  MalformedFileException(const std::string& msg)
232  : Exception(msg)
233  {
234  }
235  };
236 }
237 
238 #endif
OpenMPCD::AssertionException
Represents an exception that is due to an assertion violation.
Definition: Exceptions.hpp:64
OpenMPCD::InvalidCallException
Exception for a forbidden function call.
Definition: Exceptions.hpp:144
OpenMPCD::Exception::Exception
Exception(const std::string &msg)
The constructor.
Definition: Exceptions.hpp:44
OpenMPCD::InvalidArgumentException::InvalidArgumentException
InvalidArgumentException(const std::string &msg)
The constructor.
Definition: Exceptions.hpp:135
OpenMPCD::NULLPointerException::NULLPointerException
NULLPointerException(const std::string &variable)
The constructor.
Definition: Exceptions.hpp:103
OpenMPCD::UnimplementedException
Exception for unimplemented functionality.
Definition: Exceptions.hpp:192
OpenMPCD::AssertionException::AssertionException
AssertionException(const std::string &assertion)
The constructor.
Definition: Exceptions.hpp:71
OpenMPCD::UnimplementedException::UnimplementedException
UnimplementedException(const std::string &msg)
The constructor.
Definition: Exceptions.hpp:199
OpenMPCD::IOException::IOException
IOException(const std::string &msg)
The constructor.
Definition: Exceptions.hpp:167
OpenMPCD::MemoryManagementException
Exception for errors in memory management.
Definition: Exceptions.hpp:208
OpenMPCD::OutOfBoundsException::OutOfBoundsException
OutOfBoundsException(const std::string &msg)
The constructor.
Definition: Exceptions.hpp:119
OpenMPCD::IOException
Error on IO.
Definition: Exceptions.hpp:160
OpenMPCD::Exception::getMessage
const std::string & getMessage() const
Returns the exception message.
Definition: Exceptions.hpp:52
OpenMPCD::DivisionByZeroException
Division by zero.
Definition: Exceptions.hpp:176
OpenMPCD::MemoryManagementException::MemoryManagementException
MemoryManagementException(const std::string &msg)
The constructor.
Definition: Exceptions.hpp:215
OpenMPCD::MalformedFileException
Represents an exception that signals a malformed file.
Definition: Exceptions.hpp:224
OpenMPCD::MalformedFileException::MalformedFileException
MalformedFileException(const std::string &msg)
The constructor.
Definition: Exceptions.hpp:231
OpenMPCD::InvalidConfigurationException
Represents an invalid configuration.
Definition: Exceptions.hpp:80
OpenMPCD::Exception
The base exception class for OpenMPCD.
Definition: Exceptions.hpp:37
OpenMPCD::OutOfBoundsException
Exception for out-of-bounds access.
Definition: Exceptions.hpp:112
OpenMPCD::InvalidCallException::InvalidCallException
InvalidCallException(const std::string &msg)
The constructor.
Definition: Exceptions.hpp:151
OpenMPCD::InvalidConfigurationException::InvalidConfigurationException
InvalidConfigurationException(const std::string &setting)
The constructor.
Definition: Exceptions.hpp:87
OpenMPCD::NULLPointerException
NULL-pointer exception.
Definition: Exceptions.hpp:96
OpenMPCD::InvalidArgumentException
Invalid argument exception.
Definition: Exceptions.hpp:128
OpenMPCD::DivisionByZeroException::DivisionByZeroException
DivisionByZeroException(const std::string &msg)
The constructor.
Definition: Exceptions.hpp:183