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

nestedSize() method from fcf::Exception class

size_t nestedSize() 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

Returns the number of nested exceptions contained within this exception.

The nestedSize method returns the count of sub-exceptions that are part of the current exception's context.

When an exception wraps another, the hierarchy is maintained. This method allows you to determine how many layers of causality are attached to the current exception object, which is useful for iterating through the error chain.

Result
size_t
- The number of nested exceptions.

Example: Counting nested exceptions

Demonstrating how to use nestedSize to iterate through a chain of wrapped exceptions.

#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. Use nestedSize() to count and loop through 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) { 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 nestedSize method is a key part of the exception inspection API, enabling programmatic traversal of error causality chains.