mirror of
https://github.com/boostorg/throw_exception.git
synced 2025-07-23 00:57:22 +02:00
Update documentation
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
////
|
||||
Copyright 2019 Peter Dimov
|
||||
Copyright 2019, 2022 Peter Dimov
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
http://www.boost.org/LICENSE_1_0.txt
|
||||
////
|
||||
@ -35,83 +35,12 @@ The macro `BOOST_THROW_EXCEPTION(x)` expands to
|
||||
`::boost::throw_exception(x, BOOST_CURRENT_LOCATION)`, passing the current source
|
||||
location.
|
||||
|
||||
[#examples]
|
||||
# Examples
|
||||
:toc:
|
||||
:toc-title:
|
||||
:idprefix:
|
||||
When integration with Boost.Exception and `boost::exception_ptr` is not needed,
|
||||
the function `boost::throw_with_location` can be used instead. It also throws
|
||||
a user-provided exception, associating it with a supplied or inferred source
|
||||
location, but does not supply the `boost::exception` base class and does not
|
||||
enable `boost::exception_ptr` support.
|
||||
|
||||
## Using BOOST_THROW_EXCEPTION
|
||||
|
||||
```
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/exception/diagnostic_information.hpp>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Using boost::throw_exception with a source location
|
||||
|
||||
```
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/exception/diagnostic_information.hpp>
|
||||
#include <stdexcept>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
|
||||
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<std::string>( i ) + " >= "
|
||||
+ boost::lexical_cast<std::string>( 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()
|
||||
{
|
||||
try
|
||||
{
|
||||
f1( 4, 3 );
|
||||
}
|
||||
catch( std::exception const & x )
|
||||
{
|
||||
std::cerr << boost::diagnostic_information( x ) << std::endl;
|
||||
}
|
||||
}
|
||||
```
|
||||
The source location of the exception thrown by `boost::throw_with_location`
|
||||
can be retrieved, after `catch(std::exception const & x)`, by using
|
||||
`boost::get_throw_location(x)`.
|
||||
|
Reference in New Issue
Block a user