NOCL  v0.1.0-2524
Modern C++ Class Library for GUI Projects
nocl::Exception Class Reference

Exception class that inherits from the standard C++ class std:exception. More...

#include <Exception.hpp>

Inheritance diagram for nocl::Exception:
Collaboration diagram for nocl::Exception:

Public Member Functions

virtual ~Exception (void)
 Destructor. More...
 
 Exception (void)
 Empty constructor. Wont have a function name, filename, nor line number. Will have the call stack. More...
 
 Exception (const std::exception &rhs)
 Copy constructor from a standard exception to preserve the text in what(). Wont have a function name, filename, nor line number. Will have the call stack where the std::exception is converted to nocl::Exception. More...
 
 Exception (const std::string &text)
 Constructor with a text description to use when what() is called. Wont have a function name, filename, nor line number. Will have the call stack. More...
 
 Exception (const std::string &func, const std::string &fname, const size_t linenum, const std::string &text)
 Normal constructor for the exception class. More...
 
std::string details (void) const
 Format the exception including the location and full call stack into a multi-line text string. More...
 
virtual const char * what (void) const noexcept override
 Return the single-line message describing this exception. More...
 

Public Attributes

std::string what_text
 Single line of text to return when what() is called. More...
 
std::string function
 Function name. Depending on how the exception was created, this may be an empty string. More...
 
std::string filename
 Filename. Depending on how the exception was created, this may be an empty string. More...
 
size_t line_number
 Line number. Depending on how the exception was created, this may be zero. More...
 
VStr call_stack
 Vector of strings showing the call stack at the time the nocl::Exception object was created. More...
 

Static Public Attributes

static std::string log_filename
 Filename where exceptions created should be logged. More...
 

Detailed Description

Exception class that inherits from the standard C++ class std:exception.

In addition to the usual what() call, these exception objects also stores the function name, filename, line number, and the full call stack where the exception was created.

To get the most details from your exceptions, make sure you use the constructor that uses the NOCL_WHERE macro to pass in all the necessary information.

For example:

if (error_detected)
{
// the NOCL_WHERE macro conveniently passes in the function name, filename, and line number to the exception object
throw nocl::Exception(NOCL_WHERE, "error detected during parsing");
}

Call details() on your exceptions to get a multi-line text description of the exception, which will include the exception text, the location where the exception happened, and the full call stack when the exception object was instantiated.

For example:

try
{
// ...do something here that may throw one of these exceptions...
}
catch (const nocl::Exception &e)
{
std::cout << e.details() << std::endl;
}

The result of calling "e.details()" would be similar to this text block:

Exception: unexpected condition
Location: read_input_file(), Processing.cpp, line #317
Call stack contains 6 items:
- /usr/bin/controller: read_input_file(std::basic_ofstream<char, std::char_traits<char> >&) +0xd2 [0x565503f240d1]
- /usr/bin/controller: start_processing(bool) +0x39 [0x565503f24299]
- /usr/bin/controller: locker() +0x9 [0x565503f242f5]
...

Constructor & Destructor Documentation

◆ ~Exception()

nocl::Exception::~Exception ( void  )
virtual

Destructor.

◆ Exception() [1/4]

nocl::Exception::Exception ( void  )

Empty constructor. Wont have a function name, filename, nor line number. Will have the call stack.

◆ Exception() [2/4]

nocl::Exception::Exception ( const std::exception &  rhs)
explicit

Copy constructor from a standard exception to preserve the text in what(). Wont have a function name, filename, nor line number. Will have the call stack where the std::exception is converted to nocl::Exception.

◆ Exception() [3/4]

nocl::Exception::Exception ( const std::string &  text)

Constructor with a text description to use when what() is called. Wont have a function name, filename, nor line number. Will have the call stack.

◆ Exception() [4/4]

nocl::Exception::Exception ( const std::string &  func,
const std::string &  fname,
const size_t  linenum,
const std::string &  text 
)

Normal constructor for the exception class.

The first 3 parameters to this constructor are normally filled out by using the the NOCL_WHERE macro. This is the constructor strongly suggested be used since it fills in all of the location details.

For example:

if (error_detected)
{
throw nocl::Exception(NOCL_WHERE, "error detected during parsing");
}
See also
NOCL_WHERE
details()

References details(), nocl::Logfile::get(), log_filename, and nocl::Logfile::ts().

Here is the call graph for this function:

Member Function Documentation

◆ details()

std::string nocl::Exception::details ( void  ) const

Format the exception including the location and full call stack into a multi-line text string.

For example:

throw nocl::Exception(NOCL_WHERE, "Unexpected EOF!");

When the exception is caught, the details could be logged similar to this:

catch (const nocl::Exception &e)
{
std::cout << e.details() << std::endl;
}

This would result in a multi-line text block such as:

Exception: Unexpected EOF!
Location: read_input_file(), Processing.cpp, line #317
Call stack contains 6 items:
- /usr/bin/controller: read_input_file(std::basic_ofstream<char, std::char_traits<char> >&) +0xd2 [0x565503f240d1]
- /usr/bin/controller: start_processing(bool) +0x39 [0x565503f24299]
- /usr/bin/controller: locker() +0x9 [0x565503f242f5]
...
Note
  • The text string will have multiple lines, but does not end in std::endl.
  • Call what() to get a single line of text (without the call stack).

References call_stack, filename, line_number, nocl::shorten_path_for_display(), and what().

Referenced by Exception(), and nocl::Application::run_message_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ what()

const char * nocl::Exception::what ( void  ) const
overridevirtualnoexcept

Return the single-line message describing this exception.

Is also used as the first line of text in the multi-line details().

References what_text.

Referenced by details().

Here is the caller graph for this function:

Member Data Documentation

◆ what_text

std::string nocl::Exception::what_text

Single line of text to return when what() is called.

Is also used as the first line of text in the multi-line details().

Referenced by what().

◆ function

std::string nocl::Exception::function

Function name. Depending on how the exception was created, this may be an empty string.

See also
details()

◆ filename

std::string nocl::Exception::filename

Filename. Depending on how the exception was created, this may be an empty string.

See also
details()

Referenced by details().

◆ line_number

size_t nocl::Exception::line_number

Line number. Depending on how the exception was created, this may be zero.

See also
details()

Referenced by details().

◆ call_stack

VStr nocl::Exception::call_stack

Vector of strings showing the call stack at the time the nocl::Exception object was created.

See also
details()

Referenced by details().

◆ log_filename

std::string nocl::Exception::log_filename
static

Filename where exceptions created should be logged.

If set, a nocl::Logfile object is created and the exception is logged with the text returned by Exception::details().

Referenced by Exception(), and nocl::Logfile::log_nocl_exceptions().


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