[boost] regarding boost::exception

4 views
Skip to first unread message

rajesh daggumati via Boost

unread,
Oct 17, 2021, 9:47:58 AM10/17/21
to bo...@lists.boost.org, rajesh daggumati
Hi Team,
I need to create an object for catched exception .
class exception: virtual public std::exception ,virtual public
boost::exception
{}

inside my application code,
try{ boost::rethrow_exception(exceptr)}
catch(std::exception &e)
{
std::exception *e = stacktoHeapObject(e);
}



static std::exception* stacktoHeapObj(std::exception* e)
{
if (dynamic_cast<std::bad_alloc*>(e))
{
return new std::bad_alloc(*(dynamic_cast<std::bad_alloc*>(e)));
}
else if (dynamic_cast<std::bad_cast*>(e))
{
return new
std::bad_cast(*(dynamic_cast<std::bad_cast*>(e)));
}
else if (dynamic_cast<std::bad_typeid*>(e))
{
return new
std::bad_typeid(*(dynamic_cast<std::bad_typeid*>(e)));
}
else if (dynamic_cast<std::bad_exception*>(e))
{
return new
std::bad_exception(*(dynamic_cast<std::bad_exception*>(e)));
}
else if (dynamic_cast<std::domain_error*>(e))
{
return new
std::domain_error(*(dynamic_cast<std::domain_error*>(e)));
}
else if (dynamic_cast<std::invalid_argument*>(e))
{
return new
std::invalid_argument(*(dynamic_cast<std::invalid_argument*>(e)));
}
else if (dynamic_cast<std::length_error*>(e))
{
return new
std::length_error(*(dynamic_cast<std::length_error*>(e)));
}
else if (dynamic_cast<std::out_of_range*>(e))
{
return new
std::out_of_range(*(dynamic_cast<std::out_of_range*>(e)));
}
else if (dynamic_cast<std::overflow_error*>(e))
{
return new
std::overflow_error(*(dynamic_cast<std::overflow_error*>(e)));
}
else if (dynamic_cast<std::range_error*>(e))
{
return new
std::range_error(*(dynamic_cast<std::range_error*>(e)));
}
else if (dynamic_cast<std::underflow_error*>(e))
{
return new
std::underflow_error(*(dynamic_cast<std::underflow_error*>(e)));
}
else if (dynamic_cast<std::runtime_error*>(e))
{
return new
std::runtime_error(*(dynamic_cast<std::runtime_error*>(e)));
}
else if (dynamic_cast<std::exception*>(e))
{
return new
std::exception(*(dynamic_cast<std::exception*>(e)));
}
}
I have coded by taking order reference of below pic.
please help me to get classes names in boost:exception which are not in
std::exception .
if they are in boost::exception only then how to catch that and send to
stackToHeapObj and what order i need to follow to add those if checks.


try
{
boost::rethrow_exception(exptr)
}
here in catch block , we don't know which exception type we have in
exptr(ie. std:: or boost:;) then How to catch them. if i use
std::exception in catch block then if exptr throws boost::exception type
then ... and viceversa..?

Please help me to clarify above queries


[image: image.png]

Vinnie Falco via Boost

unread,
Oct 17, 2021, 10:14:29 AM10/17/21
to boost@lists.boost.org List, Vinnie Falco, rajesh daggumati
On Sun, Oct 17, 2021 at 6:47 AM rajesh daggumati via Boost
<bo...@lists.boost.org> wrote:
> ...

I don't know exactly what is going on here, but I am having trouble
seeing the necessity. Maybe you could explain exactly what you are
trying to do, because this business of doing all these casts is not
something that I have seen before. Is it possible that this function
could be helpful to you?

<https://en.cppreference.com/w/cpp/error/make_exception_ptr>

Regards

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

rajesh daggumati via Boost

unread,
Oct 17, 2021, 12:20:59 PM10/17/21
to Vinnie Falco, rajesh daggumati, boost@lists.boost.org List
My application code was handled with shared_pointers.
here boost::rethrow_exception is throwing exception object (which is
created in stack..) but shared pointers need objects which are created on
heap SO for that , I am converting object from stack to heap by
copying it.(ie. classname *c1 = new classname(stack_object)).
I have typecasted all std::exceptions
I have two queries
1. I need some class whose 2 childs are std::exception and
boost::exception so that I can type cast based on if checks..
2. The exception classes which are in boost::exception and not in
std::exception


Please help me to clarify above 2 queries.

Emil Dotchevski via Boost

unread,
Oct 17, 2021, 3:01:49 PM10/17/21
to bo...@lists.boost.org, Emil Dotchevski
On Sun, Oct 17, 2021 at 9:21 AM rajesh daggumati via Boost <
bo...@lists.boost.org> wrote:

> My application code was handled with shared_pointers.
> here boost::rethrow_exception is throwing exception object (which is
> created in stack..) but shared pointers need objects which are created on
> heap SO for that , I am converting object from stack to heap by
> copying it.(ie. classname *c1 = new classname(stack_object)).


It looks like you should use std::current_exception and holding exceptions
in std::exception_ptr rather than std::shared_ptr. If using an old compiler
that does not support these, Boost provides compatible alternatives.

rajesh daggumati via Boost

unread,
Oct 18, 2021, 8:59:41 AM10/18/21
to Vinnie Falco, rajesh daggumati, boost@lists.boost.org List
try
{
BOOST_THROW_EXCEPTION(std::invalid_argument("Blah"));
}
catch (...)
{
boost::exception_ptr exception = boost::current_exception();
try {
boost::rethrow_exception(exception);
}
catch()
//exception is from std::invalid_argument do i need to give
boost::excepion &e or
std::exception &e in catch block


My application code is having boost::exception and std::exception. I need
a datatype to store both std::exception and boost::exception(other than
boost::exception_ptr as everytime I need to rethrow and catch it and need
to do everythng inside cathc block.. its not possible always)

rajesh daggumati via Boost

unread,
Oct 18, 2021, 9:00:49 AM10/18/21
to Vinnie Falco, rajesh daggumati, boost@lists.boost.org List
try
{
BOOST_THROW_EXCEPTION(std::invalid_argument("Blah"));
}
catch (...)
{
boost::exception_ptr exception = boost::current_exception();
try {
boost::rethrow_exception(exception);
}
catch()
//exception is from std::invalid_argument do i need to give
boost::excepion &e or
std::exception &e in catch block


My application code is having boost::exception and std::exception. I need
a datatype to store both std::exception and boost::exception(other than
boost::exception_ptr as everytime I need to rethrow and catch it and need
to do everythng inside cathc block.. its not possible always)


Emil Dotchevski via Boost

unread,
Oct 18, 2021, 2:44:24 PM10/18/21
to Boost, Emil Dotchevski
On Mon, Oct 18, 2021 at 6:01 AM rajesh daggumati via Boost <
bo...@lists.boost.org> wrote:
>
> try
> {
> BOOST_THROW_EXCEPTION(std::invalid_argument("Blah"));
> }
> catch (...)
> {
> boost::exception_ptr exception = boost::current_exception();
> try {
> boost::rethrow_exception(exception);
> }
> catch()
> //exception is from std::invalid_argument do i need to give
> boost::excepion &e or
> std::exception &e in catch block
>
>
> My application code is having boost::exception and std::exception. I need
> a datatype to store both std::exception and boost::exception(other than
> boost::exception_ptr as everytime I need to rethrow and catch it and need
> to do everythng inside cathc block.. its not possible always)

If you can't handle the exception completely, you can rethrow the original
exception like this:

try
{
BOOST_THROW_EXCEPTION(std::invalid_argument("Blah"));
}
catch (...)
{
boost::exception_ptr exception = boost::current_exception();
try {
boost::rethrow_exception(exception);
}
catch( std::exception & e ) {
... // partially handle std::exception
throw; // rethrow original object
}
catch( boost::exception & e ) {
... // partially handle boost::exception
throw; // rethrow original object
Reply all
Reply to author
Forward
0 new messages