How to store on exception object ?
Hello 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 ? Thank you, Timothy Madden
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
Does code below work correctly? (C++0x fashion)
=================================================================
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"
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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"
Sent: Tuesday, August 31, 2010 11:04 AM To: 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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"
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"
Sent: Tuesday, August 31, 2010 11:04 AM To: 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Tue, Aug 31, 2010 at 10:05 AM, Timothy Madden
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 :)
In trunk revision 65195 I've added non-intrusive (meaning, it works for any exception object, not only the ones wrapped with boost::enable_current_exception) exception_ptr support for MSVC 7.1 and 8.0, entirely based on an implementation Anthony Williams had posted months ago (on other MSVC versions the intrusive support is used as a fallback.) Because I made some changes and I've done only limited testing, to enable it you must #define BOOST_ENABLE_NON_INTRUSIVE_EXCEPTION_PTR. You'll also need to link libs/exception/src/clone_current_exception_msvc.cpp in your project. I'm assuming MSVC 9.0 and newer will just work as soon as the appropriate exception_info_offset value is defined in clone_current_exception_msvc.cpp. Anthony? Non-intrusive exception_ptr support for other compilers is now easy to integrate in Boost Exception too. Hopefully someone can provide suitable implementation for GCC at least (I can help with the integration.) Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
On 9/1/2010 9:21 AM, Emil Dotchevski wrote:
On Tue, Aug 31, 2010 at 10:05 AM, Timothy Madden
wrote: 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 :)
In trunk revision 65195 I've added non-intrusive (meaning, it works for any exception object, not only the ones wrapped with boost::enable_current_exception) exception_ptr support for MSVC 7.1 and 8.0, entirely based on an implementation Anthony Williams had posted months ago (on other MSVC versions the intrusive support is used as a fallback.)
Because I made some changes and I've done only limited testing, to enable it you must #define BOOST_ENABLE_NON_INTRUSIVE_EXCEPTION_PTR. You'll also need to link libs/exception/src/clone_current_exception_msvc.cpp in your project.
I'm assuming MSVC 9.0 and newer will just work as soon as the appropriate exception_info_offset value is defined in clone_current_exception_msvc.cpp. Anthony?
Non-intrusive exception_ptr support for other compilers is now easy to integrate in Boost Exception too. Hopefully someone can provide suitable implementation for GCC at least (I can help with the integration.)
I think this would be awesome ! Currently though clone_current_exception_msvc.cpp only has the offsets for cl 13.10 and 14.00, so I need the offset for 15.00 to use this. Also I would not like to tamper with the boost sources before building, so if the new offset could be committed on the trunk (since it is only activated by the #define), that would be great. ! Thank you, Timothy Madden
On Wed, Sep 1, 2010 at 2:08 PM, Timothy Madden
Currently though clone_current_exception_msvc.cpp only has the offsets for cl 13.10 and 14.00, so I need the offset for 15.00 to use this.
Non-intrusive exception_ptr support should work for MSVC 7.1, 8.0 and 9.0 now, see trunk revision 65204. Although all tests pass on my computer, I still consider this code highly experimental so you must #define BOOST_ENABLE_NON_INTRUSIVE_EXCEPTION_PTR to enable it (also, you must link clone_current_exception_msvc.cpp in your program.) Feedback and bug reports appreciated. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
Emil Dotchevski
I'm assuming MSVC 9.0 and newer will just work as soon as the appropriate exception_info_offset value is defined in clone_current_exception_msvc.cpp. Anthony?
Yes. Your implementation looks reasonable. It's worth noting that it won't work on WIN64 though. Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
On Mon, Sep 6, 2010 at 2:33 AM, Anthony Williams
Emil Dotchevski
writes: I'm assuming MSVC 9.0 and newer will just work as soon as the appropriate exception_info_offset value is defined in clone_current_exception_msvc.cpp. Anthony?
Yes. Your implementation looks reasonable. It's worth noting that it won't work on WIN64 though.
Thanks once again for the original code. Good to know it's 32-bit only. What is the reason for that? Is the 64-bit MSVC exception handling code totally different, or does it only differ by offsets and other magic numbers? Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
Emil Dotchevski
On Mon, Sep 6, 2010 at 2:33 AM, Anthony Williams
wrote: Emil Dotchevski
writes: I'm assuming MSVC 9.0 and newer will just work as soon as the appropriate exception_info_offset value is defined in clone_current_exception_msvc.cpp. Anthony?
Yes. Your implementation looks reasonable. It's worth noting that it won't work on WIN64 though.
Thanks once again for the original code.
Good to know it's 32-bit only. What is the reason for that? Is the 64-bit MSVC exception handling code totally different, or does it only differ by offsets and other magic numbers?
The offsets and magic numbers are different. Also, some of the values that are pointers in the 32-bit code are 32-bit offsets from a separately-supplied base pointer in the 64-bit code. Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
On Mon, Sep 6, 2010 at 2:44 PM, Anthony Williams
Emil Dotchevski
writes: On Mon, Sep 6, 2010 at 2:33 AM, Anthony Williams
wrote: Emil Dotchevski
writes: I'm assuming MSVC 9.0 and newer will just work as soon as the appropriate exception_info_offset value is defined in clone_current_exception_msvc.cpp. Anthony?
Yes. Your implementation looks reasonable. It's worth noting that it won't work on WIN64 though.
Thanks once again for the original code.
Good to know it's 32-bit only. What is the reason for that? Is the 64-bit MSVC exception handling code totally different, or does it only differ by offsets and other magic numbers?
The offsets and magic numbers are different. Also, some of the values that are pointers in the 32-bit code are 32-bit offsets from a separately-supplied base pointer in the 64-bit code.
Can you elaborate on this? Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
Emil Dotchevski
On Mon, Sep 6, 2010 at 2:44 PM, Anthony Williams
wrote: Emil Dotchevski
writes: On Mon, Sep 6, 2010 at 2:33 AM, Anthony Williams
wrote: Emil Dotchevski
writes: I'm assuming MSVC 9.0 and newer will just work as soon as the appropriate exception_info_offset value is defined in clone_current_exception_msvc.cpp. Anthony?
Yes. Your implementation looks reasonable. It's worth noting that it won't work on WIN64 though.
Thanks once again for the original code.
Good to know it's 32-bit only. What is the reason for that? Is the 64-bit MSVC exception handling code totally different, or does it only differ by offsets and other magic numbers?
The offsets and magic numbers are different. Also, some of the values that are pointers in the 32-bit code are 32-bit offsets from a separately-supplied base pointer in the 64-bit code.
Can you elaborate on this?
Some of the structures are unchanged in size and layout on 64-bit Windows. This means that pointers no longer fit where they did previously, because pointers are now 64-bits instead of 32-bits. Instead of a pointer, those fields now contain 32-bit offsets from a base pointer which is stored separately. Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
On 8/31/2010 6:04 PM, Steven Watanabe wrote:
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
It looks like that only works as long as every throw site uses enable_current_exception to throw.
On Aug 31, 2010, at 10:00 AM, Timothy Madden wrote:
On 8/31/2010 6:04 PM, Steven Watanabe wrote:
AMDG
Timothy Madden wrote:
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
It looks like that only works as long as every throw site uses enable_current_exception to throw.
Yes, that's a limit of the library-only approach of Boost.Exception. On the other hand, unless you have compiler support (C++0x has exception_ptr too), it's the best you'll get. Sebastian
participants (6)
-
Anthony Williams
-
Binglong Xie
-
Emil Dotchevski
-
Sebastian Redl
-
Steven Watanabe
-
Timothy Madden