### Get Throw Location Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/doc/html/throw_exception Retrieves the source location associated with an exception. It checks if the exception was thrown using boost::throw_with_location or if it directly contains boost::exception information. ```cpp template boost::source_location get_throw_location( E const & e ); ``` -------------------------------- ### Basic boost::throw_with_location Usage in C++ Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/doc/html/throw_exception Demonstrates the fundamental use of boost::throw_with_location. When no explicit source location is provided, the macro captures the call site automatically. This requires including boost/throw_exception.hpp and boost/core/verbose_terminate_handler.hpp for exception handling and reporting. ```cpp #include #include #include int f1( int x ) { if( x < 0 ) { boost::throw_with_location( std::invalid_argument( "f1: x cannot be negative" ) ); } return x; } int main() { std::set_terminate( boost::core::verbose_terminate_handler ); return f1( -4 ); } ``` -------------------------------- ### Boost throw_exception Overload Synopsis in C++ Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/doc/html/throw_exception Provides the C++ synopsis for the 'boost::throw_exception' function overloads, including those that accept a 'boost::source_location'. This is relevant for projects where exceptions might be disabled or for integrating with the latest Boost library features. ```cpp #include #include #include namespace boost { #if defined( BOOST_NO_EXCEPTIONS ) BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_location const & loc ); // user defined #else template BOOST_NORETURN void throw_exception( E const & e ); template BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & loc ); #endif } // namespace boost #define BOOST_THROW_EXCEPTION(x) \ ::boost::throw_exception(x, BOOST_CURRENT_LOCATION) namespace boost { template BOOST_NORETURN void throw_with_location( E && e, boost::source_location const & loc = BOOST_CURRENT_LOCATION ); template boost::source_location get_throw_location( E const & e ); } // namespace boost ``` -------------------------------- ### Throw Exception with Boost.ThrowException and Source Location Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/doc/html/throw_exception Demonstrates using `BOOST_THROW_EXCEPTION` to throw a `std::runtime_error` and capture diagnostic information, including the source location. This is useful for unified exception handling and debugging. ```cpp #include #include #include #include void f() { BOOST_THROW_EXCEPTION( std::runtime_error( "Unspecified runtime error" ) ); } int main() { try { f(); } catch( std::exception const & x ) { std::cerr << boost::diagnostic_information( x ) << std::endl; } } ``` -------------------------------- ### Throw Exception with Boost.ThrowException and Explicit Source Location Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/doc/html/throw_exception Illustrates using `boost::throw_exception` with an explicit `boost::source_location` passed to a helper function. This allows accurate tracking of the logical throw point, even when the exception is thrown from a different function, integrating with `boost::exception` and `boost::diagnostic_information`. ```cpp #include #include #include #include #include #include BOOST_NORETURN BOOST_NOINLINE void throw_index_error( std::size_t i, std::size_t n, boost::source_location const & loc ) { std::string msg = "Index out of range: " + boost::lexical_cast( i ) + " >= " + boost::lexical_cast( n ); boost::throw_exception( std::out_of_range( msg ), loc ); } void f1( std::size_t i, std::size_t n ) { if( i >= n ) { throw_index_error( i, n, BOOST_CURRENT_LOCATION ); } } void f2( std::size_t i, std::size_t n ) { if( i >= n ) { throw_index_error( i, n, BOOST_CURRENT_LOCATION ); } } int main() { std::set_terminate( boost::core::verbose_terminate_handler ); f1( 0, 3 ); f2( 4, 3 ); } ``` -------------------------------- ### Throw with Source Location Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/doc/html/throw_exception Throws an exception, ensuring that the provided source location is associated with it. If exceptions are not available, it defaults to calling boost::throw_exception. ```cpp template BOOST_NORETURN void throw_with_location( E && e, boost::source_location const & loc = BOOST_CURRENT_LOCATION ); ``` -------------------------------- ### Throw Exception with Implicit Source Location (C++) Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/index Demonstrates using boost::throw_with_location to throw an exception where the source location is automatically captured. This requires including boost/throw_exception.hpp and boost/core/verbose_terminate_handler.hpp. The function implicitly captures the location of the call to boost::throw_with_location. ```cpp #include #include #include int f1( int x ) { if( x < 0 ) { boost::throw_with_location( std::invalid_argument( "f1: x cannot be negative" )); } return x; } int main() { std::set_terminate( boost::core::verbose_terminate_handler ); return f1( -4 ); } ``` -------------------------------- ### Throw Exception with Source Location Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/index Declares the `throw_exception` overload that includes source location information. This version allows associating a `boost::source_location` with the thrown exception, aiding in debugging. The behavior depends on the availability of exceptions and specific preprocessor definitions. ```cpp #if defined( BOOST_NO_EXCEPTIONS ) BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_location const & loc ); // user defined #else template BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & loc ); #endif ``` -------------------------------- ### boost::throw_with_location with Explicit Source Location in C++ Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/doc/html/throw_exception Illustrates using boost::throw_with_location with an explicitly provided source location. Helper functions like 'throw_invalid_argument' can take a 'boost::source_location' argument, defaulting to BOOST_CURRENT_LOCATION. This allows exception locations to point to the call site rather than within the helper function, aiding in pinpointing the error origin. ```cpp #include #include #include BOOST_NORETURN BOOST_NOINLINE void throw_invalid_argument( char const * msg, boost::source_location const & loc ) { boost::throw_with_location( std::invalid_argument( msg ), loc ); } int f1( int x, boost::source_location const & loc = BOOST_CURRENT_LOCATION ) { if( x < 0 ) { throw_invalid_argument( "f1: x cannot be negative", loc ); } return x; } int f2( int x, boost::source_location const & loc = BOOST_CURRENT_LOCATION ) { if( x < 0 ) { throw_invalid_argument( "f2: x cannot be negative", loc ); } return x; } int main() { std::set_terminate( boost::core::verbose_terminate_handler ); return f1( 3 ) + f2( -11 ); } ``` -------------------------------- ### Throw Exception without Source Location Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/index Defines the `throw_exception` function for throwing exceptions. It handles cases where exceptions are disabled by the user or when Boost.Exception integration is required. The function signature varies based on whether exceptions are enabled. ```cpp #if defined( BOOST_NO_EXCEPTIONS ) BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined #else template BOOST_NORETURN void throw_exception( E const & e ); #endif ``` -------------------------------- ### Throw Exception (with and without Exceptions) Source: https://www.boost.org/doc/libs/latest/libs/throw_exception/doc/html/throw_exception Defines the throw_exception function. If Boost exceptions are disabled, it declares a user-defined function. Otherwise, it throws the provided exception object, potentially augmenting it with boost::exception capabilities and source location information. ```cpp #if defined( BOOST_NO_EXCEPTIONS ) BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined #else template BOOST_NORETURN void throw_exception( E const & e ); #endif ``` ```cpp #if defined( BOOST_NO_EXCEPTIONS ) BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_location const & loc ); // user defined #else template BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & loc ); #endif ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.