1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief A header file for the main Inference Engine exception
7 * \file ie_exception.hpp
19 * @def THROW_IE_EXCEPTION
20 * @brief A macro used to throw the exception with a notable description
22 #define THROW_IE_EXCEPTION\
23 throw InferenceEngine::details::InferenceEngineException(__FILE__, __LINE__)\
27 * @brief Uses assert() function if NDEBUG is not defined, InferenceEngine exception otherwise
30 #define IE_ASSERT(EXPRESSION)\
31 if (!(EXPRESSION)) throw InferenceEngine::details::InferenceEngineException(__FILE__, __LINE__) << "AssertionFailed: " << #EXPRESSION // NOLINT
38 NullStream & operator << (const T &obj) noexcept {
42 NullStream & operator<< (std::ostream & (*manip)(std::ostream &)) noexcept {
47 #define IE_ASSERT(EXPRESSION)\
48 assert((EXPRESSION)); NullStream()
51 namespace InferenceEngine {
56 * @brief The InferenceEngineException class implements the main Inference Engine exception
58 class InferenceEngineException : public std::exception {
59 mutable std::string errorDesc;
60 StatusCode status_code = static_cast<StatusCode>(0);
63 std::shared_ptr<std::stringstream> exception_stream;
64 bool save_to_status_code = false;
68 * @brief A C++ std::exception API member
69 * @return An exception description with a file name and file line
71 const char *what() const noexcept override {
72 if (errorDesc.empty() && exception_stream) {
73 errorDesc = exception_stream->str();
75 errorDesc += "\n" + _file + ":" + std::to_string(_line);
78 return errorDesc.c_str();
82 * @brief A constructor. Creates an InferenceEngineException object from a specific file and line
83 * @param filename File where exception has been thrown
84 * @param line Line of the exception emitter
86 InferenceEngineException(const std::string &filename, const int line)
87 : _file(filename), _line(line) {
91 * @brief noexcept required for copy ctor
92 * @details The C++ Standard, [except.throw], paragraph 3 [ISO/IEC 14882-2014]
94 InferenceEngineException(const InferenceEngineException & that) noexcept {
95 errorDesc = that.errorDesc;
96 status_code = that.status_code;
99 exception_stream = that.exception_stream;
103 * @brief A stream output operator to be used within exception
104 * @param arg Object for serialization in the exception message
107 InferenceEngineException& operator<<(const T &arg) {
108 if (save_to_status_code) {
109 auto can_convert = status_code_assign(arg);
110 save_to_status_code = false;
111 if (can_convert.second) {
112 this->status_code = can_convert.first;
116 if (!exception_stream) {
117 exception_stream.reset(new std::stringstream());
119 (*exception_stream) << arg;
124 * @brief Manipulator to indicate that next item has to be converted to StatusCode to save
125 * @param iex InferenceEngineException object
127 friend InferenceEngineException& as_status(InferenceEngineException& iex) {
128 iex.save_to_status_code = true;
133 * @brief A stream output operator to catch InferenceEngineException manipulators
134 * @param manip InferenceEngineException manipulator to call
136 InferenceEngineException& operator<<(InferenceEngineException& (*manip)(InferenceEngineException &)) {
140 /** @brief Check if it has StatusCode value */
141 bool hasStatus() const {
142 return this->status_code == 0 ? false : true;
145 /** @brief Get StatusCode value */
146 StatusCode getStatus() const {
147 return this->status_code;
151 std::pair<StatusCode, bool> status_code_assign(const StatusCode& status) {
152 return {status, true};
155 template <typename T>
156 std::pair<StatusCode, bool> status_code_assign(const T &) {
157 return {static_cast<StatusCode>(0), false};
161 InferenceEngineException& as_status(InferenceEngineException& iex);
163 static_assert(std::is_nothrow_copy_constructible<InferenceEngineException>::value,
164 "InferenceEngineException must be nothrow copy constructible");
165 } // namespace details
166 } // namespace InferenceEngine