CppCommon  1.0.4.1
C++ Common Library
exceptions.cpp
Go to the documentation of this file.
1 
9 #include "errors/exceptions.h"
10 
11 #include <sstream>
12 
13 namespace CppCommon {
14 
15 const char* Exception::what() const noexcept
16 {
17  try
18  {
19  if (_cache.empty())
20  string();
21  return _cache.c_str();
22  }
23  catch (...)
24  {
25  return "Out of memory!";
26  }
27 }
28 
29 std::string Exception::string() const
30 {
31  if (_cache.empty())
32  {
33  std::stringstream stream;
34  stream << "Exception: " << _message << std::endl;
35  std::string location = _location.string();
36  if (!location.empty())
37  stream << "Source location: " << location << std::endl;
38  _cache = stream.str();
39  }
40  return _cache;
41 }
42 
43 std::string SystemException::string() const
44 {
45  if (_cache.empty())
46  {
47  std::stringstream stream;
48  stream << "System exception: " << _message << std::endl;
49  stream << "System error: " << _system_error << std::endl;
50  stream << "System message: " << _system_message << std::endl;
51  std::string location = _location.string();
52  if (!location.empty())
53  stream << "Source location: " << location << std::endl;
54  _cache = stream.str();
55  }
56  return _cache;
57 }
58 
59 } // namespace CppCommon
virtual std::string string() const
Get string from the current exception.
Definition: exceptions.cpp:29
const char * what() const noexcept override
Get string identifying exception.
Definition: exceptions.cpp:15
SourceLocation _location
Exception location.
Definition: exceptions.h:74
std::string _message
Exception message.
Definition: exceptions.h:72
std::string _cache
Cached exception string.
Definition: exceptions.h:70
const SourceLocation & location() const noexcept
Get exception location.
Definition: exceptions.h:51
std::string string() const
Get the string from the current source location.
int _system_error
System error code.
Definition: exceptions.h:148
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
Exceptions definition.
C++ Common project definitions.
Definition: token_bucket.h:15