mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
mxException.hpp
Go to the documentation of this file.
1/** \file mxException.hpp
2 * \author Jared R. Males (jaredmales@gmail.com)
3 * \brief Declares and defines the mxlib exception class.
4 * \ingroup error_handling_files
5 *
6 */
7
8//***********************************************************************//
9// Copyright 2015, 2016, 2017 Jared R. Males (jaredmales@gmail.com)
10//
11// This file is part of mxlib.
12//
13// mxlib is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// mxlib is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25//***********************************************************************//
26
27#ifndef __mxException__
28#define __mxException__
29
30#include <exception>
31#include <sstream>
32
33#include "mxError.hpp"
34
35namespace mx
36{
37namespace err
38{
39
40/// The mxlib exception class
41/** Provides a rich error report via the standard what().
42 * \ingroup exceptions
43 */
44class mxException : public std::exception
45{
46
47 protected:
48 /// Contains the what() string
49 char m_whatstr[4096];
50
51 /// The source of the exception, such as stdlib or cfitisio or the function name
52 std::string m_source{ "" };
53
54 /// The mxlib error code
55 int m_code{ 0 };
56
57 /// The name of the error code
58 std::string m_codeName{ "" };
59
60 /// The errno error code (only used if non-zero)
61 int m_errno{ 0 };
62
63 /// The source file where the exception originated
64 std::string m_file{ "" };
65
66 /// The line number of the file where the exception originated
67 int m_line{ 0 };
68
69 /// The long explanation of the error
70 std::string m_explanation{ "" };
71
72 public:
73 /// Default constructor
74 mxException() noexcept
75 {
76 build_what();
77 }
78
79 /// Copy constructor
80 mxException( const mxException &e ) noexcept
81 : m_source( e.m_source ), m_code( e.m_code ), m_codeName( e.m_codeName ), m_file( e.m_file ),
82 m_line( e.m_line ), m_explanation( e.m_explanation )
83 {
84 build_what();
85 }
86
87 /// Construct and fill in each of the values, except errno
88 mxException( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
89 const int &ec, ///< [in] the error code
90 const std::string &emnem, ///< [in] the name of the error code
91 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
92 const int &line, ///< [in] the line number where the exception was thrown
93 const std::string &expl ///< [in] the explanation for why the exception was thrown
94 )
95 : m_source( esrc ), m_code( ec ), m_codeName( emnem ), m_errno( 0 ), m_file( efile ), m_line( line ),
96 m_explanation( expl )
97 {
98 build_what();
99 }
100
101 /// Construct and fill in each of the values, including errno
102 mxException( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
103 const int &ec, ///< [in] the error code
104 const std::string &emnem, ///< [in] the name of the error code
105 const int &en, ///< [in] the value of errno
106 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
107 const int &line, ///< [in] the line number where the exception was thrown
108 const std::string &expl ///< [in] the explanation for why the exception was thrown
109 )
110 : m_source( esrc ), m_code( ec ), m_codeName( emnem ), m_errno( en ), m_file( efile ), m_line( line ),
111 m_explanation( expl )
112 {
113 build_what();
114 }
115
116 /// Assignment operator
117 mxException &operator=( const mxException &e ) noexcept
118 {
119 m_source = e.m_source;
120 m_code = e.m_code;
121 m_codeName = e.m_codeName;
122 m_errno = e.m_errno;
123 m_file = e.m_file;
124 m_line = e.m_line;
125 m_explanation = e.m_explanation;
126
127 build_what();
128
129 return *this;
130 }
131
132 /// Destructor
133 virtual ~mxException() throw()
134 {
135 }
136
137 /// Build the what string.
138 /** Must be called after updating any values, since the what() method is const const.
139 */
140 virtual void build_what()
141 {
142 std::ostringstream s;
143 s.str( "" );
144
145 if( m_explanation != "" )
146 s << " " << m_explanation;
147 if( m_source != "" )
148 s << "\n source: " << m_source;
149 if( m_code != 0 )
150 {
151 s << "\n code: " << m_code;
152 if( m_codeName != "" )
153 s << " (" << m_codeName << ")";
154 }
155 if( m_errno != 0 )
156 {
157 s << "\n errno: " << m_errno << " (" << errno_CodeToName( m_errno ) << ")";
158 }
159 if( m_file != "" )
160 s << "\n in file: " << m_file;
161 if( m_line != 0 )
162 s << "\n at line: " << m_line;
163
164 if( m_errno != 0 )
165 {
166 s << "\n " << strerror( m_errno );
167 }
168 snprintf( m_whatstr, sizeof( m_whatstr ), "%s", s.str().c_str() );
169 }
170
171 /// Return the details of the exception as a single string.
172 virtual const char *what() const noexcept
173 {
174 return m_whatstr;
175 }
176};
177
178/// mxException for invalid arguments
179/**
180 * \ingroup exceptions
181 */
183{
184 public:
185 invalidarg( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
186 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
187 const int &line, ///< [in] the line number where the exception was thrown
188 const std::string &expl ///< [in] the explanation for why the exception was thrown
189 )
190 : mxException( esrc, MXE_INVALIDARG, MXE_INVALIDARG_NAME, efile, line, expl )
191 {
192 }
193};
194
195/// mxException for invalid config settings
196/**
197 * \ingroup exceptions
198 */
200{
201 public:
203 const std::string &esrc, ///< [in] the source of the exception, typically the class and function
204 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
205 const int &line, ///< [in] the line number where the exception was thrown
206 const std::string &expl ///< [in] the explanation for why the exception was thrown
207 )
208 : mxException( esrc, MXE_INVALIDCONFIG, MXE_INVALIDCONFIG_NAME, efile, line, expl )
209 {
210 }
211};
212
213/// mxException for not implemented features
214/**
215 * \ingroup exceptions
216 */
217class notimpl : public mxException
218{
219 public:
220 notimpl( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
221 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
222 const int &line, ///< [in] the line number where the exception was thrown
223 const std::string &expl ///< [in] the explanation for why the exception was thrown
224 )
225 : mxException( esrc, MXE_NOTIMPL, MXE_NOTIMPL_NAME, efile, line, expl )
226 {
227 }
228};
229
230/// mxException for parameters which aren't set
231/**
232 * \ingroup exceptions
233 */
235{
236 public:
237 paramnotset( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
238 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
239 const int &line, ///< [in] the line number where the exception was thrown
240 const std::string &expl ///< [in] the explanation for why the exception was thrown
241 )
242 : mxException( esrc, MXE_PARAMNOTSET, MXE_PARAMNOTSET_NAME, efile, line, expl )
243 {
244 }
245};
246
247/// mxException for not found
248/**
249 * \ingroup exceptions
250 */
251class notfound : public mxException
252{
253 public:
254 notfound( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
255 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
256 const int &line, ///< [in] the line number where the exception was thrown
257 const std::string &expl ///< [in] the explanation for why the exception was thrown
258 )
259 : mxException( esrc, MXE_NOTFOUND, MXE_NOTFOUND_NAME, efile, line, expl )
260 {
261 }
262};
263
264/// mxException for a size error
265/**
266 * \ingroup exceptions
267 */
268class sizeerr : public mxException
269{
270 public:
271 sizeerr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
272 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
273 const int &line, ///< [in] the line number where the exception was thrown
274 const std::string &expl ///< [in] the explanation for why the exception was thrown
275 )
276 : mxException( esrc, MXE_SIZEERR, MXE_SIZEERR_NAME, efile, line, expl )
277 {
278 }
279};
280
281/// mxException for an allocation error
282/**
283 * \ingroup exceptions
284 */
285class allocerr : public mxException
286{
287 public:
288 allocerr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
289 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
290 const int &line, ///< [in] the line number where the exception was thrown
291 const std::string &expl ///< [in] the explanation for why the exception was thrown
292 )
293 : mxException( esrc, MXE_ALLOCERR, MXE_ALLOCERR_NAME, efile, line, expl )
294 {
295 }
296};
297
298/// mxException for errors on opening a file
299/**
300 * \ingroup exceptions
301 */
302class fileoerr : public mxException
303{
304 public:
305 fileoerr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
306 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
307 const int &line, ///< [in] the line number where the exception was thrown
308 const std::string &expl ///< [in] the explanation for why the exception was thrown
309 )
310 : mxException( esrc, MXE_FILEOERR, MXE_FILEOERR_NAME, efile, line, expl )
311 {
312 }
313 fileoerr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
314 const int &en, ///< [in] the value of errno
315 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
316 const int &line, ///< [in] the line number where the exception was thrown
317 const std::string &expl ///< [in] the explanation for why the exception was thrown
318 )
319 : mxException( esrc, MXE_FILEOERR, MXE_FILEOERR_NAME, en, efile, line, expl )
320 {
321 }
322};
323
324/// mxException for errors reading from a file
325/**
326 * \ingroup exceptions
327 */
328class filererr : public mxException
329{
330 public:
331 filererr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
332 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
333 const int &line, ///< [in] the line number where the exception was thrown
334 const std::string &expl ///< [in] the explanation for why the exception was thrown
335 )
336 : mxException( esrc, MXE_FILERERR, MXE_FILERERR_NAME, efile, line, expl )
337 {
338 }
339 filererr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
340 const int &en, ///< [in] the value of errno
341 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
342 const int &line, ///< [in] the line number where the exception was thrown
343 const std::string &expl ///< [in] the explanation for why the exception was thrown
344 )
345 : mxException( esrc, MXE_FILERERR, MXE_FILERERR_NAME, en, efile, line, expl )
346 {
347 }
348};
349
350/// mxException for errors writing to a file
351/**
352 * \ingroup exceptions
353 */
354class filewerr : public mxException
355{
356 public:
357 filewerr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
358 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
359 const int &line, ///< [in] the line number where the exception was thrown
360 const std::string &expl ///< [in] the explanation for why the exception was thrown
361 )
362 : mxException( esrc, MXE_FILEWERR, MXE_FILEWERR_NAME, efile, line, expl )
363 {
364 }
365 filewerr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
366 const int &en, ///< [in] the value of errno
367 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
368 const int &line, ///< [in] the line number where the exception was thrown
369 const std::string &expl ///< [in] the explanation for why the exception was thrown
370 )
371 : mxException( esrc, MXE_FILEWERR, MXE_FILEWERR_NAME, en, efile, line, expl )
372 {
373 }
374};
375
376/// mxException for errors closing a file
377/**
378 * \ingroup exceptions
379 */
380class filecerr : public mxException
381{
382 public:
383 filecerr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
384 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
385 const int &line, ///< [in] the line number where the exception was thrown
386 const std::string &expl ///< [in] the explanation for why the exception was thrown
387 )
388 : mxException( esrc, MXE_FILECERR, MXE_FILECERR_NAME, efile, line, expl )
389 {
390 }
391 filecerr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
392 const int &en, ///< [in] the value of errno
393 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
394 const int &line, ///< [in] the line number where the exception was thrown
395 const std::string &expl ///< [in] the explanation for why the exception was thrown
396 )
397 : mxException( esrc, MXE_FILECERR, MXE_FILECERR_NAME, en, efile, line, expl )
398 {
399 }
400};
401
402/// mxException for errors returned by a library call
403/**
404 * \ingroup exceptions
405 */
406class liberr : public mxException
407{
408 public:
409 liberr( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
410 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
411 const int &line, ///< [in] the line number where the exception was thrown
412 const std::string &expl ///< [in] the explanation for why the exception was thrown
413 )
414 : mxException( esrc, MXE_LIBERR, MXE_LIBERR_NAME, efile, line, expl )
415 {
416 }
417};
418
419/// mxException for an exception
420/**
421 * \ingroup exceptions
422 */
424{
425 public:
426 exceptthrown( const std::string &esrc, ///< [in] the source of the exception, typically the class and function
427 const std::string &efile, ///< [in] the source file in which the exception occurred, normally __FILE__
428 const int &line, ///< [in] the line number where the exception was thrown
429 const std::string &expl ///< [in] the explanation for why the exception was thrown
430 )
431 : mxException( esrc, MXE_EXCEPTTHROWN, MXE_EXCEPTTHROWN_NAME, efile, line, expl )
432 {
433 }
434};
435
436/// Throw an exception. This macro takes care of the file and line.
437/** \ingroup exceptions
438 */
439#define mxThrowException( extype, src, expl ) throw extype( src, __FILE__, __LINE__, expl );
440
441#define mxThrowExceptionErrno( extype, en, src, expl ) throw extype( src, en, __FILE__, __LINE__, expl );
442
443} // namespace err
444} // namespace mx
445
446#endif // mxException_hpp
mxException for an allocation error
allocerr(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
mxException for an exception
exceptthrown(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
mxException for errors closing a file
filecerr(const std::string &esrc, const int &en, const std::string &efile, const int &line, const std::string &expl)
filecerr(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
mxException for errors on opening a file
fileoerr(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
fileoerr(const std::string &esrc, const int &en, const std::string &efile, const int &line, const std::string &expl)
mxException for errors reading from a file
filererr(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
filererr(const std::string &esrc, const int &en, const std::string &efile, const int &line, const std::string &expl)
mxException for errors writing to a file
filewerr(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
filewerr(const std::string &esrc, const int &en, const std::string &efile, const int &line, const std::string &expl)
mxException for invalid arguments
invalidarg(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
mxException for invalid config settings
invalidconfig(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
mxException for errors returned by a library call
liberr(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
The mxlib exception class.
std::string m_file
The source file where the exception originated.
std::string m_source
The source of the exception, such as stdlib or cfitisio or the function name.
mxException(const std::string &esrc, const int &ec, const std::string &emnem, const int &en, const std::string &efile, const int &line, const std::string &expl)
Construct and fill in each of the values, including errno.
mxException(const mxException &e) noexcept
Copy constructor.
mxException & operator=(const mxException &e) noexcept
Assignment operator.
int m_line
The line number of the file where the exception originated.
std::string m_codeName
The name of the error code.
char m_whatstr[4096]
Contains the what() string.
mxException(const std::string &esrc, const int &ec, const std::string &emnem, const std::string &efile, const int &line, const std::string &expl)
Construct and fill in each of the values, except errno.
virtual ~mxException()
Destructor.
int m_errno
The errno error code (only used if non-zero)
std::string m_explanation
The long explanation of the error.
int m_code
The mxlib error code.
virtual const char * what() const noexcept
Return the details of the exception as a single string.
virtual void build_what()
Build the what string.
mxException() noexcept
Default constructor.
mxException for not found
notfound(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
mxException for not implemented features
notimpl(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
mxException for parameters which aren't set
paramnotset(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
mxException for a size error
sizeerr(const std::string &esrc, const std::string &efile, const int &line, const std::string &expl)
std::string errno_CodeToName(int ec)
Return the macro name and a message for a standard errno code.
Definition mxError.cpp:133
#define MXE_EXCEPTTHROWN
An exception was thrown.
#define MXE_FILERERR
An error occurred while reading from a file.
#define MXE_INVALIDCONFIG
A config setting was invalid.
#define MXE_INVALIDARG
An argument was invalid.
#define MXE_LIBERR
An error was returned by a library.
#define MXE_ALLOCERR
An error occurred during memory allocation.
#define MXE_SIZEERR
A size was invalid or calculated incorrectly.
#define MXE_FILEWERR
An error occurred while writing to a file.
#define MXE_PARAMNOTSET
A parameter was not set.
#define MXE_FILECERR
An error occurred while closing a file.
#define MXE_NOTIMPL
A component or technique is not implemented.
#define MXE_FILEOERR
An error occurred while opening a file.
#define MXE_NOTFOUND
An item was not found.
The mxlib error reporting system.
The mxlib c++ namespace.
Definition mxError.hpp:40