CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
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"
14
15#include <exception>
16#include <string>
17#include <utility>
18
20
23#define throwex throw __LOCATION__ +
24
25namespace CppCommon {
26
28
33class Exception : public std::exception
34{
35public:
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;
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
68protected:
70 mutable std::string _cache;
72 std::string _message;
75};
76
79{
80public:
82};
83
86{
87public:
89};
90
93{
94public:
96};
97
100{
101public:
103};
104
107{
108public:
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)
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
146protected:
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.
Exception & operator=(const Exception &)=default
SourceLocation _location
Exception location.
Definition exceptions.h:74
const std::string & message() const noexcept
Get exception message.
Definition exceptions.h:49
std::string _message
Exception message.
Definition exceptions.h:72
friend T && operator+(const SourceLocation &location, T &&instance)
Link exception with source location.
Definition exceptions.h:65
std::string _cache
Cached exception string.
Definition exceptions.h:70
const SourceLocation & location() const noexcept
Get exception location.
Definition exceptions.h:51
Exception(Exception &&)=default
Exception(const Exception &)=default
virtual ~Exception()=default
Exception & operator=(Exception &&)=default
Exception(const std::string &message="")
Default class constructor.
Definition exceptions.h:40
Runtime exception.
Definition exceptions.h:93
Security exception.
Definition exceptions.h:100
std::string string() const
Get the string from the current source location.
System exception.
Definition exceptions.h:107
int _system_error
System error code.
Definition exceptions.h:148
const std::string & system_message() const noexcept
Get system error message.
Definition exceptions.h:141
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.
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
SystemException()
Create system exception based on the last system error code.
Definition exceptions.h:110
Exceptions inline implementation.
C++ Common project definitions.
Source location definition.
System error definition.