LocalException.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <exception>
6 #include <sstream>
7 #include <string>
8 
9 namespace armarx
10 {
11  /**
12  * \class LocalException
13  * \ingroup Exceptions
14  * \brief Standard exception type for ArmarX, that should not be transmitted via Ice.
15  *
16  *
17  * Offers a backtrace to where the exception was thrown.<br/>
18  * All local exceptions must inherit from this class.
19  */
20  class LocalException: public std::exception
21  {
22  public:
23  /**
24  * @brief LocalException Constructor
25  */
26  LocalException();
27  /**
28  * @brief LocalException Copy Constructor
29  */
30  LocalException(const LocalException& e) noexcept;
31  /**
32  * @brief LocalException Constructor taking a \p reason string as parameter
33  */
34  LocalException(const std::string& reason) :
35  reason(reason), backtrace(""), output_buffer("")
36  {
37  backtrace = std::runtime_error(generateBacktrace());
38  setReason(reason);
39  }
40 
41  /**
42  * @brief LocalException Destructor
43  */
44  ~LocalException() noexcept override = default;
45 
46  /**
47  * @return string containing the reason of the exception
48  */
49  const char* what() const noexcept override;
50  /**
51  * @return the exception name
52  */
53  virtual std::string name() const
54  {
55  return "armarx::LocalException";
56  }
57 
58  /**
59  * @param reason string containing an explanation about the reason why the exception occured
60  */
61  void setReason(const std::string& reason);
62 
63 
64  /**
65  * @return string containing the reason without backtrace
66  */
67  std::string getReason() const;
68 
69  /**
70  * @return string containing the backtrace
71  */
72  static std::string generateBacktrace();
73 
74  template<typename T>
75  LocalException& operator <<(const T& message)
76  {
77  std::stringstream ss;
78  ss << reason.what() << message;
79  reason = std::runtime_error(ss.str());
80  return *this;
81  }
82 
83  template<typename T>
84  LocalException& operator <<(const std::vector<T>& message)
85  {
86  // the operator <<(ostream, vector) is defined in two headers
87  // the implementations behave differently.
88  // here we explicitly select the operator we want
89  std::operator <<(std::stringstream(reason.what()), message);
90  return *this;
91  }
92 
93  private:
94  /**
95  * Generates an explanatory string by concatenating all LocalException
96  * information and stores it in armarx::LocalException::resultingMessage.
97  */
98  std::string generateOutputString() const;
99  /**
100  * @brief reason is a std::runtime_error object holding an explanation of why the exception
101  * was thrown
102  */
103  std::runtime_error reason;
104  /**
105  * @brief backtrace is a std::runtime_error object containing the backtrace from where
106  * the exception was thrown
107  */
108 
109  std::runtime_error backtrace;
110 
111  /**
112  * @brief output_buffer is a std::runtime_error object that is filled and kept when calling what() for combining
113  * the reason and the backtrace
114  */
115  mutable std::runtime_error output_buffer;
116  };
118  "LocalException must be nothrow copy constructible");
119 
120  /**
121  * \ingroup Exceptions
122  * Print the output of common armarx exceptions.
123  *
124  * Handles exception types like armarx::LocalException, armarx::UserException,
125  * IceUtil::Exception and std::exception.
126  *
127  * If available, the backtrace is printed out.
128  *
129  * example:
130  * \code
131  * try{
132  * //...your code
133  * }
134  * catch(...)
135  * {
136  * handleExceptions();
137  * // your special exception handling
138  * }
139  * \endcode
140  */
141  extern "C" void handleExceptions();
142 
143  /**
144  * \ingroup Exceptions
145  * Generates a string from an exception with additional information for some exceptions, like Ice::NoObjectFactoryException.
146 
147  * example:
148  * \code
149  * try{
150  * //...your code
151  * }
152  * catch(...)
153  * {
154  * ARMARX_ERROR_S << GetHandledExceptionString();
155  * // your special exception handling
156  * }
157  */
158  std::string GetHandledExceptionString();
159 }
message
message(STATUS "Boost-Library-Dir: " "${Boost_LIBRARY_DIRS}") message(STATUS "Boost-LIBRARIES
Definition: CMakeLists.txt:8
StringHelpers.h
armarx::GetHandledExceptionString
std::string GetHandledExceptionString()
Definition: Exception.cpp:147
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
std::operator<<
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)
std
Definition: Application.h:66
armarx::operator<<
std::ostream & operator<<(std::ostream &os, const PythonApplicationManager::Paths &paths)
Definition: PythonApplicationManager.cpp:221
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:141
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28