PStack  2.0
Stack trace printer for MSVC and GCC binaries
psystem::exception::base_exception Class Reference

Base exception class from which all PSystem exceptions must inherit. More...

#include <base_exception.hpp>

Inheritance diagram for psystem::exception::base_exception:
Collaboration diagram for psystem::exception::base_exception:

Construction / Destruction

 base_exception (std::string file, std::string function, uint32_t line) noexcept
 Construct a base_exception instance with no message. More...
 
template<typename... MessageArgs>
 base_exception (std::string file, std::string function, uint32_t line, MessageArgs &&...message) noexcept
 Construct a base_exception instance with a formatted message. More...
 
virtual ~base_exception () noexcept
 Destroy the exception (supports run-time polymorphism).
 

Virtual Method Overrides

virtual char const * what () const noexcept override
 Return a message describing the exception that occurred. More...
 

Public Accessors

std::string const & function () const noexcept
 Return the function where the exception occurred. More...
 
uint32_t line_number () const noexcept
 Return the line on which the exception occurred. More...
 
std::string const & source_file () const noexcept
 Return the name of the source file where the exception occurred. More...
 

Private Utilities

static std::string format_message (char const *msg,...) noexcept
 A functional sprintf() replacement. More...
 

Member Data

std::string const m_what
 A message describing the exception condition.
 
std::string const m_function
 The function where the exception occurred.
 
uint32_t const m_line_number
 The line number (in m_source_file) where the exception occurred.
 
std::string const m_source_file
 The source file where the exception occurred.
 

Additional Inherited Members

- Protected Member Functions inherited from psystem::not_assignable
not_assignableoperator= (not_assignable const &) const =delete
 Delete the basic assignment operator.
 
constexpr not_assignable () noexcept
 A simple CTOR suitable for noexcept and constexpr uses.
 
 ~not_assignable () noexcept
 A simple CTOR suitable for noexcept uses.
 

Detailed Description

Base exception class from which all PSystem exceptions must inherit.

This exception should never be thrown directly. It provides base functionality for all exceptions raised in PSystem-based applications. It is important to note that this derives from std::exception, and applications should not throw something unless it derives from this (unless they want to see std::terminate get called).

Derived classes may allow construction with no message, a simple text message, or a printf style formatted message. Here is a simple example:

class my_exception : public psystem::exception::base_exception
{
public:
template <typename... MessageArgs>
ui_exception(
std::string const& file, std::string const& function, uint32_t line,
MessageArgs&&... message) noexcept
file, function, line,
std::forward<MessageArgs>(message)...) { }
};
// This can be thrown with just a string:
throw my_exception(
__FILE__, __FUNCTION__, __LINE__, "Some error occurred!");
// ... or with formatted details:
throw my_exception(
__FILE__, __FUNCTION__, __LINE__,
"Some system error occurred -- %d (%s)",
errno, strerror(errno));
Author
Matt Bisson
Date
26 November, 2007 – 5 August, 2014
Since
PSystem 1.0
Version
PSystem 2.0
Todo:
Every exception should support the copy and assignment operations.
Todo:
Remove not_assignable

Constructor & Destructor Documentation

psystem::exception::base_exception::base_exception ( std::string  file,
std::string  function,
uint32_t  line 
)
protectednoexcept

Construct a base_exception instance with no message.

Parameters
[in]fileThe source file where the exception occurred.
[in]functionThe function in the source where the exception occurred.
[in]lineThe line in file where the exception occurred.
template<typename... MessageArgs>
psystem::exception::base_exception::base_exception ( std::string  file,
std::string  function,
uint32_t  line,
MessageArgs &&...  message 
)
inlineprotectednoexcept

Construct a base_exception instance with a formatted message.

Parameters
[in]fileThe source file where the exception occurred.
[in]functionThe function in the source where the exception occurred.
[in]lineThe line in file where the exception occurred.
[in]messageThis should be in a printf message/args style calling convention.

Member Function Documentation

std::string psystem::exception::base_exception::format_message ( char const *  msg,
  ... 
)
staticprivatenoexcept

A functional sprintf() replacement.

This method is slightly more useful to us than sprintf() because it returns a std::string directly. This lets us skip all the details of finding the right size for an output buffer, allocating it, passing it around, and so on. All we're really interested in is a std::string.

Parameters
[in]msgThe printf format string.
[in]...A variable length cstdarg argument pack used to populate the result, given msg.
Returns
This returns a std::string instance of the formatted string.

This could be implemented with perfect forwarding, but I don't want to include that much implementation detail in the header. With a non-template function, I can keep the definition away from every compilation unit.

Todo:
Should this be moved somewhere more global?
std::string const & psystem::exception::base_exception::function ( ) const
noexcept

Return the function where the exception occurred.

Returns
A reference to the std::string containing the function name. The reference is valid until the exception instance is destroyed.
uint32_t psystem::exception::base_exception::line_number ( ) const
noexcept

Return the line on which the exception occurred.

Returns
A line number relative to the start of the source file.
See also
source_file()
std::string const & psystem::exception::base_exception::source_file ( ) const
noexcept

Return the name of the source file where the exception occurred.

Returns
A reference to the std::string containing the file name. The reference is valid until the exception instance is destroyed.
char const * psystem::exception::base_exception::what ( ) const
overridevirtualnoexcept

Return a message describing the exception that occurred.

Returns
A pointer to a null-terminated C string with content related to the exception. This points to valid memory until the underlying base_exception instance is destroyed.

Reimplemented in psystem::exception::system_exception.


The documentation for this class was generated from the following files: