Throwing bad_exception when calling current_exception()
Jonathan Wakely
jwakely@redhat.com
Mon Dec 2 14:39:00 GMT 2019
On 15/11/19 20:02 +0300, Евгений Никишин via libstdc++ wrote:
>
>Link https://en.cppreference.com/w/cpp/error/current_exception provides the following description of current_exception() :
>>If called during exception handling (typically, in a catch clause), captures the current exception object and creates an std::exception_ptr that holds either a copy or a reference to that exception object (depending on the implementation).
>>...
>>If the implementation of this function requires copying the captured exception object and its copy constructor throws an exception, the returned pointer will hold a reference to the exception thrown. If the copy constructor of the thrown exception object also throws, the returned pointer may hold a reference to an instance of std::bad_exception to break the endless loop.
>I am trying to find out if the implementation of current_exception() in GCC7 copies captured exception object, or just returns the reference to an already existing object. So far, I think that GCC implements the second case. I've tried to check it by executing the following code:
>class my_copy_exception :public exception
>{public:
> my_copy_exception (): exception (){}
> my_copy_exception (const my_copy_exception& other):
> exception(other){throw my_copy_exception();}constchar* what ()constthrow(){return"my_copy_exception";}};int main(){try{throw my_copy_exception();}catch(const exception& e){
> cout << e.what()<< endl;
> exception_ptr eptr = current_exception();try{
> rethrow_exception(eptr);}catch(const std::exception& en){
> cout << en.what()<< endl;
> exception_ptr eptrn = current_exception();
>
> cout <<(eptr == eptrn)<< endl;}}}
>It produces the following output:
>my_copy_exception
>my_copy_exception
>1
>Whether it is possible to claim that there is no copying of the exception object? If not, how to make current_exception() throw bad_exception ?
Sorry for the delay, I didn't receive this email due to some problems
with our email systems.
The libstdc++ implementation never copies the exception. A reference
to the existing one is returned (and the reference count increased).
More information about the Libstdc++
mailing list