FCF_EXCEPTION_DECLARE macro
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
When you use this macro, the framework:
- Generates a class with the name provided in
am_type. - Creates constructors capable of receiving up to 9 parameters, as specified by
am_argCount. - Sets up a message template using
am_message, which supports runtime argument substitution via the${{i}}$syntax (e.g.,${{1}}$,${{2}}$). - Allows the exception to be caught as a
fcf::Exception while retaining its specific metadata.
Arguments
- The name of the new exception class to be created.
- The unique string identifier for this error type, returned by registeredName().
- The message template. Use placeholders like ${{1}}$, ${{2}}, etc., to inject argument values into the final message.
- 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
VPSDime is an industry leading VPS hosting company that provides virtualized server services with high performance, availability and friendly support.