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

FCF_EXCEPTION_DECLARE macro

FCF_EXCEPTION_DECLARE (TYPE am_type, const char* am_name, const char* am_message, int am_argCount)

Package: fcfBasis

File: macro.hpp ( link_to_the_declaration_in_the_file )

Available from version: 1.0.1

Declares a custom exception class derived from fcf::Exception, supporting message templates and multiple arguments.

This macro is used to define a new exception type that inherits from fcf::Exception. It automates the creation of the class, including constructors for a specified number of arguments.

When you use this macro, the framework:

  1. Generates a class with the name provided in am_type.
  2. Creates constructors capable of receiving up to 9 parameters, as specified by am_argCount.
  3. Sets up a message template using am_message, which supports runtime argument substitution via the ${{i}}$ syntax (e.g., ${{1}}$, ${{2}}$).
  4. Allows the exception to be caught as a fcf::Exception while retaining its specific metadata.

Arguments

TYPE am_type
- The name of the new exception class to be created.

const char* am_name
- The unique string identifier for this error type, returned by registeredName().

const char* am_message
- The message template. Use placeholders like ${{1}}$, ${{2}}, etc., to inject argument values into the final message.

int am_argCount
- The number of arguments (from 0 to 9) that the exception's constructors will support.

Example: Declaring and throwing a custom exception with arguments

#include <iostream> // Define an implementation macro to include the implementation section in header files. #define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> // 1. Declare a custom exception class named 'MathError'. // It will support 2 arguments and use them in the message template. // The placeholders ${{1}}$ and ${{2}}$ will be replaced by the values passed to the constructor. FCF_EXCEPTION_DECLARE(MathError, "MATH_ERROR", "Math operation failed: arg1={${{1}}$}, arg2={${{2}}$}", 2); int main() { try { // 2. Throw the custom exception with two arguments. // The values "Division by zero" and 0 will be injected into the message. throw MathError(__FILE__, __LINE__, "Division by zero", 0); } catch (const fcf::Exception& e) { // 3. Catch the exception by its base class reference. std::cout << "=== Exception Caught ===" << std::endl; // 4. Access and print exception metadata. std::cout << "Error Name: " << e.name() << std::endl; std::cout << "Formatted Msg: " << e.message() << std::endl; std::cout << "Location: " << e.position() << std::endl; } return 0; }

Output:

=== Exception Caught === Error Name: MATH_ERROR Formatted Msg: Math operation failed: arg1={Division by zero}, arg2={0} Location: /home/user/application/main.cpp:16