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
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 theraise 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
- Determines whether the raise method should throw an exception or simply record the error state.
- A pointer to a string containing the registered name of the error, if an error has been recorded.
- A pointer to an fcf::Exception object that holds the detailed error metadata.
Methods
- 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.
- Resets the error state to a clean, default condition.
- Triggers an error by populating the current error state and optionally throwing the specified exception.
- Checks if the error object currently contains an error state.
- Checks if the error object is in a clean (success) state.
Example: Manual Error Handling and Loud Exceptions
Demonstrates how to use
#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
VPSDime is an industry leading VPS hosting company that provides virtualized server services with high performance, availability and friendly support.