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

Exception() method from fcf::Exception class

[CONSTRUCTOR] Exception()
[CONSTRUCTOR] Exception(const fcf::Exception& a_exception)
[CONSTRUCTOR] Exception(const fcf::Exception& a_exception, bool a_copySub)
template <typename ...TArgPack>
[CONSTRUCTOR] Exception(const char* a_file, unsigned int a_line, const TArgPack&... a_argPack)
template <typename ...TArgPack>
[CONSTRUCTOR] Exception(const char* a_shortMessage, const char* a_patternMessage, const char* a_file, unsigned int a_line, const TArgPack&... a_argPack)
template <typename ...TArgPack>
[CONSTRUCTOR] Exception(const fcf::Exception& a_subException, const char* a_file, unsigned int a_line, const TArgPack&... a_argPack)
template <typename ...TArgPack>
[CONSTRUCTOR] Exception(const fcf::Exception& a_subException, const char* a_shortMessage, const char* a_patternMessage, const char* a_file, unsigned int a_line, const TArgPack&... a_argPack)

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

Initializes a new instance of the fcf::Exception class. Depending on the arguments provided, it can create a simple error, an error with an associated exception object, or a complex exception with formatted messages and arguments.

The fcf::Exception class provides a robust mechanism for error reporting and exception handling. Its constructors allow for various levels of detail, enabling developers to provide rich context during error propagation:

  • Constructor without parameters: Creates an exception object without information; in this case, the name() method returns an empty string.
  • Constructor without exception name: If a constructor is used without specifying a_shortMessage, an exception with the name "FCF_EXCEPTION" is created, and the message pattern is set to "${{1}}$" (i.e., it outputs the first passed argument TArgPack)
  • Wrapped Mode: Allows the creation of an exception that wraps an existing fcf::Exception object (if the a_subexception parameter is passed). This is useful for maintaining a chain of causality where one error leads to another.
  • Formatted Mode (Template): The most powerful constructor. It accepts a message pattern (using ${{1}}$, ${{2}}$, etc.) and a variadic pack of arguments. This allows for highly dynamic and informative error messages that are formatted only when the message is actually requested.

Arguments

const char* a_file
- The file name where the exception occurred.

unsigned int a_line
- The line number where the exception occurred.

const TArgPack&... a_argPack
- Variadic arguments used to substitute placeholders (e.g., ${{1}}$) in the error message pattern.

const fcf::Exception& a_exception
- The existing fcf::Exception object to be wrapped.

bool a_copySub = true
- Flag indicating whether the sub-exceptions of the wrapped exception should also be copied. If the parameter is not specified, it defaults to true.

const char* a_shortMessage
- A custom short message for the exception.

const char* a_patternMessage
- A message pattern containing placeholders (e.g., ${{1}}$) that will be filled by the provided arguments.

const fcf::Exception& a_subException
- The base fcf::Exception object used to create a new exception with additional context.

Example: Demonstrating various constructor overloads

An example showing the use of the default constructor, the template constructor for formatted messages, and how to catch and inspect the resulting exception.

#include <iostream> #define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> int main(int argc, char* argv[]) { // 1. Constructor with custom message and arguments (Template Mode) // The arguments "Arg1" and 42 will be used to fill the pattern ${{1}}$ and ${{2}}$ fcf::Exception ex2("Custom Error", "Message: ${{1}}$ and ${{2}}$", __FILE__, __LINE__, "Arg1", 42); // 2. Throwing the exception and catching it to verify the output try { std::cout << "Throwing exception..." << std::endl; throw ex2; } catch (const fcf::Exception& e) { // Accessing the formatted message and position std::cout << "Caught: " << e.message() << std::endl; std::cout << "Position: " << e.position() << std::endl; } return 0; }

Output:

Throwing exception... Caught: Message: Arg1 and 42 Position: /home/user/application/main.cpp:8

The fcf::Exception constructors allow for flexible and detailed error reporting, from simple flags to complex, argument-driven messages.