[boost] conversion of catched exception to shared_ptr

3 views
Skip to first unread message

rajesh daggumati via Boost

unread,
Oct 19, 2021, 10:27:22 AM10/19/21
to boost@lists.boost.org List, rajesh daggumati
rajesh daggumati <rajeshdag...@gmail.com>
09:59 (4 hours ago)
to boost-users
HI,
please help me to solve below issue.
Shared pointers are used in my code.

class Exception : public virtual std::exception,public virtual
boost::exception
{
---
};
class ExceptionTestException : public Exception
{
---
};
boost::shared_ptr<Exception> exceptionptr_t;




try
{
Boost_Throw_function(ExceptionTestException("Hiii"));
}
catch(...)
{
boost::exception_ptr exceptionptr = boost::current_exception();
try
{
boost::rethrow(exceptionptr)
}
catch(boost::exception &e)
{
//here i need to assign this 'e' to shared pointer but this e is not in
heap so then how to assign this e to sharedptr(exceptionptr_t).
1 way is :
boost::exception *ep = new ExceptionTestException(e);
exceptionptr_t(ep);
here we know that exception_ptr is having ExceptionTestException but
in some cases, we dont know which type of excepiton we have in
exception_ptr in runtime.
then how to assign to shared_ptr

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Richard Hodges via Boost

unread,
Oct 19, 2021, 11:42:43 AM10/19/21
to boost@lists.boost.org List, Richard Hodges
On Tue, 19 Oct 2021 at 16:27, rajesh daggumati via Boost <
bo...@lists.boost.org> wrote:

> rajesh daggumati <rajeshdag...@gmail.com>
> 09:59 (4 hours ago)
> to boost-users
> HI,
> please help me to solve below issue.
> Shared pointers are used in my code.
>

Shared pointers are used in almost all code, but never for exceptions.
The tool for the job is std::exception_ptr, which was first modelled in
boost with boost::exception_ptr:

https://en.cppreference.com/w/cpp/error/exception_ptr
https://www.boost.org/doc/libs/1_77_0/libs/exception/doc/boost-exception.html

This problem has already been solved, for all implementations, correctly.
Creating your own solution is counterproductive and doomed to fail. After
all, you want to be solving computational problems right? Not
reimplementing what's already in the standard library.

Note that you *cannot *capture the persistent address of an exception in a
reliable way - even if the exception has already been transported into an
exception_ptr by current_exception().
It is simply not a feature provided by the language. Once thrown,
exceptions are ephemeral objects that typically occupy memory specially
reserved for the purpose by the implementation.
As far as you, the programmer is concerned, they only exist while your
catch block is executing.
Copying them at this point will lead to slicing, because you cannot
enumerate all possible concrete types for a given exception base class at
compile time.

The only way to interrogate the captured exception is to re-throw it, for
example:

void
interrogate(boost::exception_ptr ep)
{
try
{
if (ep) boost::rethrow_exception(ep);

// if we got here there was no exception
}
catch(my_custom_error& e)
{
// it was my error
}
catch(std::logic_error& e)
{
// it was a logic error
}
catch(std::runtime_error& e)
{
// it was a runtime_error
}
catch(std::exception& e)
{
// some other standard exception
}
catch(...)
{
// something else, which we can do nothing with.
}
}

Note that an exception_ptr is as cheap to copy as a shared_ptr. So you may
as well transport it by value.
In fact, under the covers it behaves the same as a shared_ptr, but with
extra magic to avoid slicing or memory corruption.

Seth via Boost

unread,
Nov 5, 2021, 1:06:39 PM11/5/21
to Richard Hodges via Boost, Seth
Op 19-10-2021 om 17:41 schreef Richard Hodges via Boost:
> The only way to interrogate the captured exception is to re-throw it, for

[...]

Awesome breakdown. Care to also post that on their question on SO?

https://stackoverflow.com/questions/69625420/how-to-assign-catched-exceptions-to-shared-pointers-in-boost-1-56-0/69633061#69633061

Obviously I posted largely the same response back then, but your text is
way more articulate!


Seth

Richard Hodges via Boost

unread,
Nov 6, 2021, 4:05:40 AM11/6/21
to bo...@lists.boost.org, Richard Hodges
On Fri, 5 Nov 2021 at 18:06, Seth via Boost <bo...@lists.boost.org> wrote:

> Op 19-10-2021 om 17:41 schreef Richard Hodges via Boost:
> > The only way to interrogate the captured exception is to re-throw it, for
>
> [...]
>
> Awesome breakdown. Care to also post that on their question on SO?


The fact that this needs to be on SO at all tells me that the best use of
the C++ standards committee would be to produce a human-readable
instruction manual for the language.

>
>
>
> https://stackoverflow.com/questions/69625420/how-to-assign-catched-exceptions-to-shared-pointers-in-boost-1-56-0/69633061#69633061
>
> Obviously I posted largely the same response back then, but your text is
> way more articulate!
>
>
> Seth
>
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
--
Richard Hodges
hodg...@gmail.com
office: +44 2032 898 513
home: +376 861 195
mobile: +376 380 212

Gavin Lambert via Boost

unread,
Nov 8, 2021, 5:19:55 PM11/8/21
to bo...@lists.boost.org, Gavin Lambert
On 6/11/2021 21:05, Richard Hodges wrote:
> The fact that this needs to be on SO at all tells me that the best use of
> the C++ standards committee would be to produce a human-readable
> instruction manual for the language.

That's what the book industry is for.
Reply all
Reply to author
Forward
0 new messages