Hi *,

there are a few recurring patterns for error handling in UNO API
implementations, three of which I'll address here:

 1. input validation/precondition checks. Violating one of those
    usually leads to an IllegalArgumentException being thrown.
 2. Runtime errors, usually signalled by a RuntimeException (this is
    comprised of a whole bunch of error conditions, ranging from logic
    errors/invariant violations to resource exhaustion)
 3. exception-less error reporting, usually via boolean return values

For all cases, one might want to see an assertion in debug builds, if
only to make sure the caller handles the error condition correctly.

So, I'd like to propose a bunch of macros, quite similar to the
OSL_ASSERT/OSL_ENSURE family, that, in the case of an error, show an
assertion in debug builds, and always throw a corresponding exception:

---%<-----------------------------------------------------------------

#define VALIDATE_AND_THROW(c, m, ifc, arg_num)  if( !(c) ) { \
           OSL_ENSURE(false, m); \
           throw ::com::sun::star::lang::IllegalArgumentException( \
           ::rtl::OUString::createFromAscii(BOOST_CURRENT_FUNCTION) + \
           ::rtl::OUString::createFromAscii(",\n"m), \
           ifc, \
           arg_num ); }

#define ENSURE_AND_THROW(c, m, ifc) if( !(c) ) { \
          OSL_ENSURE(false, m); \
          throw ::com::sun::star::uno::RuntimeException( \
          ::rtl::OUString::createFromAscii(BOOST_CURRENT_FUNCTION) + \
          ::rtl::OUString::createFromAscii(",\n"m), \
          ifc ); }

#define ENSURE_AND_RETURN(c, m)  if( !(c) ) { \
          OSL_ENSURE(false, m); \
          return false; }

---%<-----------------------------------------------------------------

Is this useful, and if yes, where to place it? It needs UNO, so osl is
too low, and it currently employs boost, which afaik rules out udk...

Feedback greatly appreciated,

-- Thorsten

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to