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

fcf::Error class

Type:
class fcf::Error

Package: fcfBasis

File: bits/PartException/ErrorDecl.hpp ( link_to_the_text_position_in_the_file )

Available from version: 1.0.1

A lightweight error container for error reporting, supporting both simple error names and full exception objects.

The fcf::Error structure allows you to report an error by throwing an exception or writing exception information to a variable without changing the function code.

Key features include:

  • Dual-mode Reporting: Can store a simple error name string or a full fcf::Exception instance for deep diagnostic inspection.
  • Configurable Severity (Loudness): The loud property determines whether an error should be silently recorded or immediately thrown as a C++ exception via the raise method.
  • Seamless Integration: Provides boolean conversion operators, allowing it to be used intuitively in conditional statements (e.g., if (error) { ... }).
  • Error Clearing: The clear method allows for the efficient reuse of error objects by resetting their state.

Properties
bool loud = false
- Determines whether the raise method should throw an exception or simply record the error state.
const char* error = 0
- A pointer to a string containing the registered name of the error, if an error has been recorded.
Exception* exception = 0
- A pointer to an fcf::Exception object that holds the detailed error metadata.
Methods
[CONSTRUCTOR] Error()
[CONSTRUCTOR] Error(bool a_loud)
[CONSTRUCTOR] Error(fcf::Exception* a_exception)
[CONSTRUCTOR] Error(bool a_loud, fcf::Exception* a_exception)
- Initializes a new fcf::Error object. It can represent a clean state, a loud error state, or an error associated with a specific fcf::Exception object.
void clear()
- Resets the error state to a clean, default condition.
template <typename TException, typename ...TArgPack>
void raise(TArgPack&& ...a_argPack)
- Triggers an error by populating the current error state and optionally throwing the specified exception.
bool operator bool() const
- Checks if the error object currently contains an error state.
bool operator!() const
- Checks if the error object is in a clean (success) state.

Example: Manual Error Handling and Loud Exceptions

Demonstrates how to use fcf::Error to capture errors without throwing, how to inspect the error name and associated exception, and how to use the raise method to trigger a loud exception.

#include <iostream> #define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> // 1. A mock function that simulates an operation that might fail. // Instead of throwing an exception directly, it uses the fcf::Error object // to capture the error state. int performOperation(int a_value, fcf::Error& a_error) { if (a_value < 0) { // 2. The raise method sets the error name and the exception object. // Since the Error object's 'loud' property isn't set to true here, it will only record the error. a_error.raise<fcf::LogicException>(__FILE__, __LINE__, "Value cannot be negative"); return 0; } return a_value + 1; } int main() { // --- Case 1: Error captured with an external exception object --- { fcf::Exception exception; // 3. Initialize Error with a pointer to an existing exception object. // This allows the Error object to 'fill' the provided exception variable. fcf::Error err(&exception); performOperation(-10, err); // 4. Check if an error occurred using the boolean operator. if (err) { std::cout << "1) Operation failed: " << err.error << std::endl; // The exception member points to the variable provided during initialization. std::cout << "Message: " << err.exception->message() << std::endl; } } // --- Case 2: Silent error recording (no associated exception object) --- { // 5. Initialize a default Error object. It will only store the error name string. fcf::Error err; performOperation(-10, err); if (err) { std::cout << "\n2) Operation failed: " << err.error << std::endl; } } // --- Case 3: Loud error (immediately throws exception) --- { // 6. Initialize Error with 'loud = true'. // When raise is called, it will throw the exception immediately. fcf::Error err(true); try { performOperation(-10, err); } catch (const std::exception& e) { std::cout << "\n3) Exception caught via try-catch:" << std::endl << e.what() << std::endl; } } return 0; }

Output:

1) Operation failed: FCF_LOGIC Message: Logic error: Value cannot be negative 2) Operation failed: FCF_LOGIC 3) Exception caught via try-catch: Logic error: Value cannot be negative Position: /home/user/application/main.cpp:13