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

nested() method from fcf::Exception class

const fcf::Exception& nested(size_t a_index) const

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

Provides access to a specific nested exception within the exception hierarchy.

The nested method allows traversing the chain of causality in an fcf::Exception object. When an exception is created by wrapping another exception, the original exception and all its subsequent sub-exceptions are stored in an internal collection.

If the requested index does not exist, a reference to an empty fcf::Exception is returned.

Arguments

size_t a_index
- The zero-based index of the sub-exception to retrieve.
Result
const fcf::Exception&
- A reference to the sub-exception at the specified index.

Example: Traversing an exception hierarchy

Demonstrating how to create a chain of exceptions and traverse them using the nested method.

#define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> #include <iostream> int main() { try { // 1. Create a bottom-level exception fcf::Exception leaf("LeafError", "Leaf error occurred", __FILE__, __LINE__, "param"); // 2. Wrap it into a middle-level exception fcf::Exception middle(leaf, "MiddleError", "Middle error occurred", __FILE__, __LINE__, "mid_param"); // 3. Wrap that into a top-level exception fcf::Exception top(middle, "TopError", "Top error occurred", __FILE__, __LINE__, "top_param"); // 4. Inspect the hierarchy std::cout << "Top exception message: " << top.message() << std::endl; std::cout << "Number of sub-exceptions: " << top.nestedSize() << std::endl; for (size_t i = 0; i < top.nestedSize(); ++i) { // Accessing sub-exceptions via nested() std::cout << " [" << i << "] " << top.nested(i).message() << std::endl; } } catch (const fcf::Exception& e) { std::cout << "Caught: " << e.what() << std::endl; } return 0; }

Output:

Top exception message: Top error occurred Number of sub-exceptions: 2 [0] Middle error occurred [1] Leaf error occurred

The nested method is essential for deep error inspection in complex systems where errors are wrapped as they propagate up the call stack.