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

raise() method from fcf::Error class

template <typename TException, typename ...TArgPack>
void raise(TArgPack&& ...a_argPack)

Class: fcf::Error

Package: fcfBasis

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

Implementation file: bits/PartException/Error.ipp ( link_to_the_implementation_in_the_file )

Available from version: 1.0.1

Triggers an error by populating the current error state and optionally throwing the specified exception.

The raise method is the primary mechanism for reporting errors using an fcf::Error object. It allows you to specify a particular exception type and provide any necessary arguments for its construction.

When called, the method performs the following actions:

  • Error Identification: It sets the error member to the registered name of the specified TException.
  • Exception Buffer Update: If the exception pointer is not null, it constructs a new instance of TException using the provided variadic arguments and assigns it to the pointed-to object.
  • Conditional Throwing: If the loud flag is set to true, the method will actually throw the newly constructed TException object.

Template arguments
typename TException - A name of the exception class that inherits from fcf::Exception
typename ...TArgPack - Types of arguments passed to the exception constructor.
  • The first argument is the string file name.
  • The second argument is the line number in the file.
  • The remaining arguments are passed to the string substitution pattern (Example: "${{1}}$: ${{2}}$")
Arguments

TArgPack&& ... a_argPack
- Variadic arguments used to construct the specified TException object.

Example: Capturing and throwing errors

An example demonstrating both 'quiet' error reporting (populating the error state) and 'loud' error reporting (throwing an exception) using the raise method.

#define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> #include <iostream> int main() { // 1. Non-loud error: Capture error state without throwing // We associate an exception buffer with the error object fcf::Exception ex; fcf::Error err(&ex); // loud is false by default std::cout << "Raising non-loud error..." << std::endl; // Use raise to populate the error state with a LogicException err.raise<fcf::LogicException>(__FILE__, __LINE__, "Data mismatch detected"); if (err) { std::cout << "Error captured: " << err.error << std::endl; std::cout << "Exception message: " << err.exception->what() << std::endl; } // 2. Loud error: Capture and throw // A loud error object is configured to throw when raise() is called fcf::Error loudErr(true); std::cout << "\nRaising loud error..." << std::endl; try { loudErr.raise<fcf::LogicException>(__FILE__, __LINE__, "Fatal system failure"); } catch (const fcf::Exception& e) { std::cout << "Caught loud error: " << e.what() << std::endl; } return 0; }

Output:

Raising non-loud error... Error captured: FCF_LOGIC Exception message: Logic error: Data mismatch detected Position: /home/user/application/main.cpp:14 Raising loud error... Caught loud error: Logic error: Fatal system failure Position: /home/user/application/main.cpp:27

The raise method provides a unified way to handle error reporting, whether you want to simply record the error for later inspection or propagate it immediately via an exception.