1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief A header file for the user input exception
7 * \file user_exception.hpp
18 * @def THROW_USER_EXCEPTION
19 * @brief A macro used to throw the exception with a notable description
21 #define THROW_USER_EXCEPTION(exitCode) \
22 throw UserException(exitCode)
25 * @class UserException
26 * @brief The UserException class implements an exception appearing as a result of user input
28 class UserException : public std::exception {
29 mutable std::string errorDesc;
30 std::shared_ptr<std::stringstream> exception_stream;
35 * @brief A C++ std::exception API member
36 * @return An exception description with a file name and file line
38 const char *what() const noexcept override {
39 if (errorDesc.empty() && exception_stream) {
40 errorDesc = exception_stream->str();
42 return errorDesc.c_str();
46 * @brief A constructor. Creates a UserException object
48 explicit UserException(int exitCode) : _exitCode(exitCode) {
51 UserException(int exitCode, std::string msg) : _exitCode(exitCode) {
56 * @brief A stream output operator to be used within exception
57 * @param arg Object for serialization in the exception message
60 UserException &operator<<(const T &arg) {
61 if (!exception_stream) {
62 exception_stream.reset(new std::stringstream());
64 (*exception_stream) << arg;
68 int exitCode() const { return _exitCode; }
71 class UserExceptions : public std::exception {
72 std::list<UserException> _list;
73 mutable std::string msg;
76 UserExceptions &operator<<(const UserException &arg) {
81 const char *what() const noexcept override {
84 if (_list.size() == 1) {
85 ss << _list.back().what();
87 auto iter = _list.begin();
88 for (size_t i = 0; i < _list.size() - 1; i++) {
89 ss << "\t* " << (*iter++).what() << std::endl;
91 ss << "\t* " << _list.back().what();
98 const std::list<UserException>& list() const {
102 bool empty() const { return _list.empty(); }