Main MRPT website > C++ reference for MRPT 1.5.6
xsexception.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifdef __cplusplus
10 #ifndef XSEXCEPTION_H
11 #define XSEXCEPTION_H
12 
13 #include <exception>
14 #include "xsresultvalue.h"
15 #include "xsstring.h"
16 
17 /*! \brief Exception class for Xsens public libraries. Inherits from std::exception
18 */
19 class XsException : public std::exception {
20 public:
21  //! \brief Copy constructor
22  XsException(XsException const& e)
23  : m_code(e.m_code)
24  , m_description(e.m_description)
25  {
26  }
27 
28  /*! \brief Initializing constructor
29  \details This constructor uses the value in \a err and the supplied \a description to create a full
30  text for when the user requests what() or text()
31  \param err The error code that the exception should report
32  \param description A description of the error. The constructor prefixes this with a textual
33  description of the error code unless prefix is false.
34  \param prefix Whether to prefix the description with a textual description of the error code or not (default is yes)
35  */
36  XsException(XsResultValue err, XsString const& description, bool prefix = true)
37  : m_code(err)
38  , m_description(description)
39  {
40  if (prefix && (m_code != XRV_OK))
41  {
42  char codeString[16];
43  sprintf(codeString, "%d: ", (int) m_code);
44  XsString rv(codeString);
45  rv << XsResultValue_toString(m_code);
46  if (!m_description.empty())
47  rv << "\tInfo: ";
48  rv.append(m_description);
49  m_description = rv;
50  }
51  }
52 
53  /*! \brief Initializing constructor
54  \param description A description of the error.
55  */
56  explicit XsException(XsString const& description)
57  : m_code(XRV_ERROR)
58  , m_description(description)
59  {
60  }
61 
62  //! \brief Destructor
63  virtual ~XsException() throw()
64  {
65  }
66 
67  //! \brief Assignment operator, copies \a e to this
68  XsException& operator = (XsException const& e)
69  {
70  m_code = e.m_code;
71  m_description = e.m_description;
72  return *this;
73  }
74 
75  //! \brief Returns the error value supplied during construction
76  inline XsResultValue code() const throw()
77  {
78  return m_code;
79  }
80 
81  //! \brief Returns a description of the error that occurred as a char const*
82  inline char const* what() const throw()
83  {
84  return m_description.c_str();
85  }
86 
87  //! \brief Returns a description of the error that occurred as a XsString
88  inline XsString const& text() const throw()
89  {
90  return m_description;
91  }
92 
93  /*! \brief Throw/raise the exception object
94  \note Override this in inheriting classes to make sure the proper type of exception is raised
95  */
96  virtual void raise() const
97  {
98  throw *this;
99  }
100 
101 private:
102  XsResultValue m_code; //!< The supplied error code
103  XsString m_description; //!< The supplied description, prefixed with a description of the error code
104 };
105 
106 #endif // file guard
107 #endif // __cplusplus guard
Operation was performed successfully.
Definition: xsens_std.h:32
XSTYPES_DLL_API const char * XsResultValue_toString(XsResultValue result)
XsResultValue
Xsens result values.
Definition: xsresultvalue.h:26
Definition: inftrees.h:28
int BASE_IMPEXP sprintf(char *buf, size_t bufSize, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
Definition: os.cpp:191
A generic error occurred.
Definition: xsens_std.h:58
struct XsString XsString
Definition: xsstring.h:34



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019