Boost logo

Boost :

From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2021-09-30 13:35:13


On 9/30/21 4:33 PM, Andrey Semashev wrote:
> On 9/30/21 1:12 PM, rajesh daggumati via Boost wrote:
>> Hi Team,
>> PFB code snippet:
>> try
>> {
>> BOOST_THROW_EXCEPTION(std::invalid_argument("Blah"));
>> }
>> catch(...)
>> {
>> boost::exception_ptr exception = boost::current_exception();
>> const std::invalid_arguement* p;
>> try{ boost :: rethrow_exception(exceptionptr) }
>> catch(std::invalid_argument &e)
>> {
>> p = dynamic_cast<const std::invalid_argument*>(&e)
>> }
>> const std::invalid_argument* actualStdPtr =
>> ExceptionHelper::ExtractConstPtr<std::invalid_argument>(exceptionptr);
>> EXPECT_TRUE(((void*)p) == ((void*)actualStdPtr)) << "Same
>> Object"; //this condition is getting failed.
>> }}
>> static const t* ExtractConstPtr(boost::exception_ptr ep)
>> {
>> try {
>> boost::rethrow_excepton(ep);
>> }
>> catch(T &e){
>> const T* exceptType = dynamic_cast<const T*>(&e);
>> return exceptType;
>> }
>> catch (std::exception & e){
>> const T* exceptType = dynamic_cast<const T*>(&e);
>> return exceptType;
>> }
>> }
>> //EXPECT_TRUE(((void*)p) == ((void*)actualStdPtr)) -->what will be the
>> reason to get failed as I do same thing in both cases.
>
> Throwing an exception (which is part of rethrow_excepton) creates a copy
> of the exception (http://eel.is/c++draft/except.throw#3). So the object
> referred to by exception_ptr and the object you receive in the catch
> block are different objects. Same with different catch blocks - every
> time you catch an exception, you catch a new object.

To be more accurate - every time you throw an exception, you catch a new
object. Which is what happens in your example.

> Also, the pointer to the caught exception (i.e. the one you receive in
> the catch block) is dangling as soon as you leave the catch block, as
> the exception is destroyed at this point. It may compare equal between
> different catch blocks by accident, but in general it is invalid to use
> it like you do.
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk