Boost logo

Boost Users :

Subject: Re: [Boost-users] How to store on exception object ?
From: Binglong Xie (binglongx_at_[hidden])
Date: 2010-08-31 15:00:28


The code below compiles on VS2010.
Interestingly, operator != is not defined for std::exception_ptr.
=========================================================================
#include <cassert>
#include <exception>

std::exception_ptr g_ex;

void callback_cpp_impl(); // may throw
void call_C( void (*callback)() ) {}

void new_callback()
{
  try
  {
     callback_cpp_impl(); // may throw
  }
  catch(...)
  {
     assert(g_ex==nullptr);
     g_ex = std::current_exception();
  }
}
void foo()
{
  try
  {
    g_ex = nullptr;
    call_C(&new_callback);
    if( !(nullptr==g_ex) )
      std::rethrow_exception(g_ex);
  }
  catch(...) // normal catch as if the exception is thrown locally.
  {
  }
}

--------------------------------------------------
From: "Timothy Madden" <terminatorul_at_[hidden]>
Sent: Tuesday, August 31, 2010 1:05 PM
To: <boost-users_at_[hidden]>
Subject: Re: [Boost-users] How to store on exception object ?

> On 8/31/2010 7:01 PM, Binglong Xie wrote:
>> Does code below work correctly? (C++0x fashion)
>> =================================================================
> My project uses Visual Studio 2008 and these C++0x functions do not
> work. Nice to see that they are about the enter the standard, though :)
> And also, Visual Studio 2010 looks like it has these.
>
>> std::exception_ptr g_ex; //global variable is no good, may be wrapped
>> into callback params.
>>
>> void my_callback()
>> {
>> try
>> {
>> callback_cpp_impl(); // may throw
>> }
>> catch(...)
>> {
>> assert(g_ex==nullptr);
>> g_ex = std::current_exception();
>> }
>> }
>>
>> void foo()
>> {
>> try
>> {
>> g_ex = nullptr;
>> call_C(&my_callback);
>> if( g_ex!=nullptr )
>> std::rethrow_exception(g_ex);
>> }
>> catch... // normal catch as if the exception is thrown locally.
>> }
>>
>>
>>
>> --------------------------------------------------
>> From: "Steven Watanabe" <watanabesj_at_[hidden]>
>> Sent: Tuesday, August 31, 2010 11:04 AM
>> To: <boost-users_at_[hidden]>
>> Subject: Re: [Boost-users] How to store on exception object ?
>>
>>> AMDG
>>>
>>> Timothy Madden wrote:
>>>> In a callback function I need to store and save an exception object
>>>> for until the callback is finished and the control is returned to my
>>>> application code. The callback is invoked by the C language code in
>>>> minizip library from zlib-1.2.3 contrib directory, and I need to
>>>> throw the exception when control returns to my application invoking
>>>> minizip functions.
>>>>
>>>> This is true for any exception that might get thrown and caught in my
>>>> callback, so I would like to do that for any exception type.
>>>>
>>>> However how can I store a thrown object for later re-throw without
>>>> the actual exception type ? Is there a way to do that ?
>>>
>>> I think this is what boost::exception_ptr is for.
>>> http://www.boost.org/libs/exception/doc/exception_ptr.html
>>>
>>> In Christ,
>>> Steven Watanabe
>>>
>>> _______________________________________________
>>> Boost-users mailing list
>>> Boost-users_at_[hidden]
>>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net