[fdo-dev] Standard exceptions handling practice

Mateusz Loskot mateusz at loskot.net
Fri Sep 1 15:29:25 EDT 2006


Greg Boone wrote:
> I would write something like...
> 
> try
> {
>     // operation that throws exception derived
>     // from standard ex. type std::exception
>     bar();
> }
> catch ( FdoException* ex )
> {
>     throw FdoException::Create(
>         NlsMsgGet(XYZ_123_FAIL_BAR, "bar() failed"), ex);
> }
> catch ( std::exception &e )
> {
>     FdoExceptionP cause = 
> 	
> FdoException::Create((FdoString*)(FdoStringP(e.what())));
>     throw FdoException::Create(
> 		NlsMsgGet(XYZ_123_FAIL_BAR, "bar() failed"), cause);
> }

Yes, it's good especially for wrapping whole function body.
But if one want to wrap single operation that may throw known
exception, then std::exception catch should be enough,
for example (note, it's just a general one, for illustration purpose):

void foo()
{
   s="123456";
   try
   {
      int i = boost::lexical_cast<int>(s);
   }
   catch(boost::bad_lexical_cast& e)
   {
      throw FdoException::Create(...);
   }

   //
   // .. rest of long function body
   //

}


What do you think about adding overloaded
version of FdoException::Create() to accept std::exception?
This would be provide more elegant catch of standard exceptions

For example:

FdoException* FdoException::Create(FdoString* message,
                                   std::exceptipn const& stdex)
{
        // Compile/Concatenate/Generate/whatever a message here
        // message + stdex.what()

	return new FdoException(message);
}


Then client's catch could look as follows

catch(boost::bad_lexical_cast& e)
{
   throw FdoException::Create(
      NlsMsgGet(XYZ_123_FAIL_BAR, "foo and bar failed"),
      e);
}

Or something similar, for example to compile FdoException from
another FdoException-derived type + std::exception.
Also, message concatenation is not important, but would useful.

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net




More information about the Fdo_dev mailing list