CppCommon  1.0.4.1
C++ Common Library
exceptions.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_ERRORS_EXCEPTIONS_H
10 #define CPPCOMMON_ERRORS_EXCEPTIONS_H
11 
12 #include "errors/system_error.h"
13 #include "system/source_location.h"
14 
15 #include <exception>
16 #include <string>
17 #include <utility>
18 
20 
23 #define throwex throw __LOCATION__ +
24 
25 namespace CppCommon {
26 
28 
33 class Exception : public std::exception
34 {
35 public:
37 
40  explicit Exception(const std::string& message = "") : _message(message), _location() {}
41  Exception(const Exception&) = default;
42  Exception(Exception&&) = default;
43  virtual ~Exception() = default;
44 
45  Exception& operator=(const Exception&) = default;
46  Exception& operator=(Exception&&) = default;
47 
49  const std::string& message() const noexcept { return _message; }
51  const SourceLocation& location() const noexcept { return _location; }
52 
54  const char* what() const noexcept override;
55 
57  virtual std::string string() const;
58 
60  friend std::ostream& operator<<(std::ostream& os, const Exception& ex)
61  { os << ex.string(); return os; }
62 
64  template<class T>
65  friend T&& operator+(const SourceLocation& location, T&& instance)
66  { instance._location = location; return std::forward<T>(instance); }
67 
68 protected:
70  mutable std::string _cache;
72  std::string _message;
75 };
76 
79 {
80 public:
82 };
83 
85 class DomainException : public Exception
86 {
87 public:
89 };
90 
93 {
94 public:
96 };
97 
100 {
101 public:
102  using Exception::Exception;
103 };
104 
107 {
108 public:
111  : SystemException(SystemError::GetLast())
112  {}
114 
117  explicit SystemException(int error)
118  : SystemException(SystemError::Description(error), error)
119  {}
121 
124  explicit SystemException(const std::string& message)
125  : SystemException(message, SystemError::GetLast())
126  {}
128 
132  explicit SystemException(const std::string& message, int error)
133  : Exception(message),
134  _system_error(error),
135  _system_message(SystemError::Description(error))
136  {}
137 
139  int system_error() const noexcept { return _system_error; }
141  const std::string& system_message() const noexcept { return _system_message; }
142 
144  std::string string() const override;
145 
146 protected:
150  std::string _system_message;
151 };
152 
153 } // namespace CppCommon
154 
155 #include "exceptions.inl"
156 
157 #endif // CPPCOMMON_ERRORS_EXCEPTIONS_H
Argument exception.
Definition: exceptions.h:79
Domain exception.
Definition: exceptions.h:86
const char * what() const noexcept override
Get string identifying exception.
Definition: exceptions.cpp:15
SourceLocation _location
Exception location.
Definition: exceptions.h:74
const std::string & message() const noexcept
Get exception message.
Definition: exceptions.h:49
Exception & operator=(Exception &&)=default
std::string _message
Exception message.
Definition: exceptions.h:72
Exception & operator=(const Exception &)=default
std::string _cache
Cached exception string.
Definition: exceptions.h:70
friend T && operator+(const SourceLocation &location, T &&instance)
Link exception with source location.
Definition: exceptions.h:65
Exception(Exception &&)=default
Exception(const Exception &)=default
virtual ~Exception()=default
const SourceLocation & location() const noexcept
Get exception location.
Definition: exceptions.h:51
Exception(const std::string &message="")
Default class constructor.
Definition: exceptions.h:40
Runtime exception.
Definition: exceptions.h:93
Security exception.
Definition: exceptions.h:100
System exception.
Definition: exceptions.h:107
int _system_error
System error code.
Definition: exceptions.h:148
SystemException(const std::string &message, int error)
Create system exception based on the given exception message and system error code.
Definition: exceptions.h:132
int system_error() const noexcept
Get system error code.
Definition: exceptions.h:139
std::string _system_message
System error message.
Definition: exceptions.h:150
std::string string() const override
Get string from the current system exception.
Definition: exceptions.cpp:43
SystemException(int error)
Create system exception based on the given system error code.
Definition: exceptions.h:117
SystemException(const std::string &message)
Create system exception based on the given exception message.
Definition: exceptions.h:124
const std::string & system_message() const noexcept
Get system error message.
Definition: exceptions.h:141
SystemException()
Create system exception based on the last system error code.
Definition: exceptions.h:110
Exceptions inline implementation.
C++ Common project definitions.
Definition: token_bucket.h:15
Source location definition.
System error definition.