FCF 2.0 development in progress...
> > > > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

what() method from fcf::Exception class

const char* what() const

Class: fcf::Exception

Package: fcfBasis

File: bits/PartException/ExceptionDecl.hpp ( link_to_the_declaration_in_the_file )

Implementation file: bits/PartException/Exception.hpp ( link_to_the_implementation_in_the_file )

Available from version: 1.0.1

Returns a string containing the full error description, including the formatted message and the source code position.

The what method is the standard interface for retrieving a detailed error description from an fcf::Exception object. It is designed to provide context for debugging and error reporting.

When called, the method performs the following operations:

  • Message Formatting: It resolves the error message by substituting all placeholders (e.g., ${{1}}$, ${{2}}$) with the actual values provided during the exception's construction.
  • Position Attachment: It automatically appends the exact source code location (file name and line number) where the exception was thrown.
  • Optimization: The method uses an internal caching mechanism. The complex string construction (formatting + position attachment) is performed only once during the first call. Subsequent calls return a pointer to the already allocated, cached string. The method is thread-safe.

Result
const char*
- A pointer to a null-terminated string containing the formatted error message and its position.

Example: Retrieving detailed exception information

Demonstrating how to catch a formatted exception and use the what method to print the complete error context.

#include <iostream> #define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> int main(int argc, char* argv[]) { try { // 1. Throw a formatted exception with arguments // The pattern "The result is ${{1}}$" will be substituted with the value 42 // The position (__FILE__, __LINE__) will be automatically appended by what() throw fcf::Exception("MathError", "The result is ${{1}}$", __FILE__, __LINE__, 42); } catch (const fcf::Exception& e) { // 2. Retrieve and print the detailed error message using what() // This will output the formatted message and the file/line position std::cout << "Caught exception:\n" << e.what() << std::endl; } return 0; }

Output:

Caught exception: The result is 42 Position: /home/user/application/main.cpp:11

The what method effectively combines dynamic message formatting with static source location information into a single, easy-to-use string.