PStack  2.0
Stack trace printer for MSVC and GCC binaries
system_exception.hpp
Go to the documentation of this file.
1 // ===-- include/psystem/exception/system_exception.hpp --- -*- C++ -*- --=== //
2 // Copyright (c) 2014 Matt Bisson. All rights reserved.
3 
18 #pragma once
19 #ifndef PSYSTEM_EXCEPTION_BASE_SYSTEM_EXCEPTION_HPP
20 #define PSYSTEM_EXCEPTION_BASE_SYSTEM_EXCEPTION_HPP
21 
22 #include "exception_ns.hpp"
23 #include "base_exception.hpp"
24 
28 
41 #define THROW_WINDOWS_EXCEPTION(err, msg) \
42  { \
43  throw psystem::exception::system_exception( \
44  __FILE__, __FUNCTION__, __LINE__, err, msg) ; \
45  }
46 
59 #define THROW_WINDOWS_EXCEPTION_F(err, msg, ...) \
60  { \
61  throw psystem::exception::system_exception( \
62  __FILE__, __FUNCTION__, __LINE__, err, msg, __VA_ARGS__) ; \
63  }
64 
66 
67 namespace psystem {
68 namespace exception {
69 
91 {
95 
111 public:
112  template <typename... MessageArgs>
114  std::string const& file,
115  std::string const& function,
116  uint32_t line,
117  DWORD error_code,
118  MessageArgs&&... message) noexcept
119  : base_exception(
120  file, function, line, std::forward<MessageArgs>(message)...),
121  m_error_code(error_code),
122  m_what(std::string(base_exception::what())
123  + ": "
124  + get_windows_error(error_code))
125  { }
126 
130 public:
133  virtual char const *what() const noexcept override;
134 
138 public:
145  DWORD error_code() const noexcept;
146 
150 private:
159  static std::string get_windows_error(DWORD error_code) noexcept;
160 
164 private:
166  DWORD const m_error_code;
167 
169  std::string const m_what;
170 };
171 
172 } // namespace exception
173 } // namespace psystem
174 
175 #endif // PSYSTEM_EXCEPTION_BASE_SYSTEM_EXCEPTION_HPP
Definition: shared_handle.hpp:782
Base exception class from which all PSystem exceptions must inherit.
Definition: base_exception.hpp:73
#define noexcept
Replace keyword with something useful.
Definition: platform.hpp:71
Declares the root of the exception class hierarchy for PSystem.
An exception type to encapsulate errors presented by the system API.
Definition: system_exception.hpp:89
static std::string get_windows_error(DWORD error_code) noexcept
Given a Windows system error code, retrieve a textual description of the error.
Definition: system_exception.cpp:52
system_exception(std::string const &file, std::string const &function, uint32_t line, DWORD error_code, MessageArgs &&...message) noexcept
Construct an exception with a formatted description of the error.
Definition: system_exception.hpp:113
Declarations required for the psystem::exception namespace.
Contains the process examination "system" and basic frameworks.
Definition: pstack_event_handler.hpp:28
std::string const m_what
Definition: system_exception.hpp:169
DWORD const m_error_code
The Windows error code that caused this exception.
Definition: system_exception.hpp:166
std::string const & function() const noexcept
Return the function where the exception occurred.
Definition: base_exception.cpp:55
DWORD error_code() const noexcept
Return the Windows error code that caused the exception.
Definition: system_exception.cpp:42
virtual char const * what() const noexcept override
Return a message describing the exception that occurred.
Definition: system_exception.cpp:32