*
a_agaga@yahoo.com:
Quote:
>
What are the alternatives to avoid copy pasting the same try catch
clauses to many methods [in order to convert exceptions]?
Macros might be one "alternative", but I think it should not be
thought as a real alternative in the C++ world.
|
Macros are the best alternative.
Theoretically you can use exception specifications and std::unexpected,
but since exception specifications are generally Bad in C++, they're not
used much (except the empty no-throw exception specification), and so
you can't really expect your compiler to implement them, even in 2006.
An exception specification based approach would go like this (off the
cuff, untried code):
typedef int QuaintException;
void myXTranslator()
{
try
{
throw;
}
catch( QuantException const& x )
{
throw std::runtime_exception( "Gnurk" ); // Translated.
}
catch( std::exception const& x )
{
throw; // As-is.
}
catch( ... )
{
std::terminate(); // Not supported.
}
}
class XTrans
{
private:
std::unexpected_handler myOldHandler;
public:
XTrans()
: myOldHandler( std::set_unexpected( ::myXTranslator ) )
{}
~XTrans()
{ std::set_unexpected( myOldHandler ); }
};
...
template< typename T >
void isRaiiObject( T& ) {} // Suppress compiler silly-warnings.
void SomeWrapperClass::someMemberFunc() throw( std::exception )
{
XTrans xt; isRaiiObject( xt );
SomeSillyVendorsApi::someEvenMoreSillyFuncWithQuai ntException();
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?