LocalException.h
Go to the documentation of this file.
1#pragma once
2
3#include <exception>
4#include <sstream>
5#include <string>
6
8
9namespace 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 /**
33 * @brief LocalException Constructor taking a \p reason string as parameter
34 */
35 LocalException(const std::string& reason) : 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 /**
52 * @return the exception name
53 */
54 virtual std::string
55 name() const
56 {
57 return "armarx::LocalException";
58 }
59
60 /**
61 * @param reason string containing an explanation about the reason why the exception occured
62 */
63 void setReason(const std::string& reason);
64
65
66 /**
67 * @return string containing the reason without backtrace
68 */
69 std::string getReason() const;
70
71 /**
72 * @return string containing the backtrace
73 */
74 static std::string generateBacktrace();
75
76 template <typename T>
77 LocalException&
78 operator<<(const T& message)
79 {
80 std::stringstream ss;
81 ss << reason.what() << message;
82 reason = std::runtime_error(ss.str());
83 return *this;
84 }
85
86 template <typename T>
87 LocalException&
88 operator<<(const std::vector<T>& message)
89 {
90 // the operator <<(ostream, vector) is defined in two headers
91 // the implementations behave differently.
92 // here we explicitly select the operator we want
93 std::operator<<(std::stringstream(reason.what()), message);
94 return *this;
95 }
96
97 private:
98 /**
99 * Generates an explanatory string by concatenating all LocalException
100 * information and stores it in armarx::LocalException::resultingMessage.
101 */
102 std::string generateOutputString() const;
103 /**
104 * @brief reason is a std::runtime_error object holding an explanation of why the exception
105 * was thrown
106 */
107 std::runtime_error reason;
108 /**
109 * @brief backtrace is a std::runtime_error object containing the backtrace from where
110 * the exception was thrown
111 */
112
113 std::runtime_error backtrace;
114
115 /**
116 * @brief output_buffer is a std::runtime_error object that is filled and kept when calling what() for combining
117 * the reason and the backtrace
118 */
119 mutable std::runtime_error output_buffer;
120 };
121
122 static_assert(std::is_nothrow_copy_constructible<LocalException>::value,
123 "LocalException must be nothrow copy constructible");
124
125 /**
126 * \ingroup Exceptions
127 * Print the output of common armarx exceptions.
128 *
129 * Handles exception types like armarx::LocalException, armarx::UserException,
130 * IceUtil::Exception and std::exception.
131 *
132 * If available, the backtrace is printed out.
133 *
134 * example:
135 * \code
136 * try{
137 * //...your code
138 * }
139 * catch(...)
140 * {
141 * handleExceptions();
142 * // your special exception handling
143 * }
144 * \endcode
145 */
146 extern "C" void handleExceptions();
147
148 /**
149 * \ingroup Exceptions
150 * Generates a string from an exception with additional information for some exceptions, like Ice::NoObjectFactoryException.
151
152 * example:
153 * \code
154 * try{
155 * //...your code
156 * }
157 * catch(...)
158 * {
159 * ARMARX_ERROR_S << GetHandledExceptionString();
160 * // your special exception handling
161 * }
162 */
163 std::string GetHandledExceptionString();
164} // namespace armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::ostream & operator<<(std::ostream &os, const PythonApplicationManager::Paths &paths)
std::string GetHandledExceptionString()
void handleExceptions()
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)