Rethrow an exception like in C++?
Rob T
alanb at ucora.com
Sun Mar 10 20:02:24 PDT 2013
On Friday, 8 March 2013 at 22:10:19 UTC, Ali Çehreli wrote:
> I heard about this idiom for the first time in Jon Kalb's talk.
> The name appears in his slides:
>
> http://exceptionsafecode.com/
>
> Very smart way of writing a single function that catches many
> different types of exceptions; does special things about them,
> like producing special error codes, and then returns those
> codes to the C world.
Thanks for the link and info!
> I am familiar with your use case: When a C layer calls C++, the
> exceptions must be caught and converted to error codes. This is
> how we do it:
>
> #define BEGIN_C_INTERFACE try {
>
> #define END_C_INTERFACE \
> } catch (const SomeType & e) { \
> // ... special code for SomeType \
> } catch (const SomeOtherType & e) { \
> // ... special code for SomeOtherType \
> \
> // ... etc. \
> \
> } catch( ... ) { \
> log("Unhandled exception"); \
> return some_generic_error_code; \
> }
>
> Then our C++ functions that are called from C are like this:
>
> extern "C"
> int my_api_func()
> {
> BEGIN_C_INTERFACE
>
> // ... the actual body of the function
>
> END_C_INTERFACE
> }
>
> Lippincott functions avoid macros and make it more explicit
> that the entire body is inside a try block.
>
> Ali
You seem to understand the use case, but why is your example not
making use of it?
I was expecting something like this ...
int errFromException()
{
try
{
throw;
}
catch (const SomeType & e)
{
// ... special code for SomeType
return errSomeType;
}
catch (const SomeOtherType & e)
{
// ... special code for SomeOtherType
return errSomeOtherType;
}
catch ( ...
{
... etc
catch( ... )
{
log("Unhandled exception");
return some_generic_error_code;
}
}
extern "C"
int my_api_func()
{
try
{
// body of my_api_func
return noerror;
}
catch(...)
{
return errFromException();
}
}
No more ugly macros and you have the ability to custom fit the
try catch block to perform any unique things that the function
may require when an exception is thrown.
--rt
More information about the Digitalmars-d-learn
mailing list