Re: advice on best use of try catch throw



aao wrote:
Send message is really not a good example, return value depends on the
message and GetError might indicate some minor problem in underling calls,
but yes you example will do


It makes sense in some context like :

void GetRect(RECT& rc) throw(CError &)
{
assert(::IsWindow(m_hWnd));

if(!:: GetWindowRect(m_hWnd, & rc))
throw(CError(GetLastError()));
}

FYI: exception specifications are a bad idea in general. The reason is that
the compiler is required to filter out all but the given exceptions and
invoke std::unexpected, which presents an additional overhead.


//my preference still would be(it does not force you user into emergency
recovery mode) :

HRESULT GetRect(RECT& rc)
{
assert(::IsWindow(m_hWnd));

if(!:: GetWindowRect(m_hWnd, & rc))
return HRESULT_FROM_WIN32(GetLastError());

return S_OK;
}

- I can't initialise a constant with the result of this function.
- It forces me to clutter my logic with the checking of returnvalues, i.e.
this is a maintenance overhead.
- It encourages less good programmers to ignore possible errors.
- Recurring checks of returnvalues are an unnecessary performance overhead.
- Exception can carry much more information than a HRESULT. If you replace
it with a different struct, the performance penalty increases further.

That said, there is also Boost.Optional:

boost::optional<CRect>
GetRect() {
CRect rc;
if( ::IsWindow(m_hWnd)
&& ::GetWindowRect( m_hWnd, &rc))
return rc;
return boost::none;
}

I would go for an assertion though, and require the programmer to check
first that the object is really associated with a window. IOW, I would
declare a failure to be a programming error.

Uli

.



Relevant Pages

  • Re: Question concerning object-oriented programming
    ... there has usually been a toString. ... I would prefer an exception thrown. ... Actual programmers are supposed to write code that verifies input. ... Educated middle-class people learn how to use language to ...
    (comp.programming)
  • Re: Structured exception information
    ... the programmers, I don't think that approach will produce good error ... involved in exception raising and exception handling in the ... But when the exceptions were reported as strings into some log ... program and can be arranged for Ada coverage rules. ...
    (comp.lang.ada)
  • Re: Rationale behind unwind-protect and double errors
    ... try/finally exception handling system. ... C++ programmers usually use destructors for the same purpose. ... handling, which does not use the mechanism you outline. ...
    (comp.lang.lisp)
  • Two questions about efficiency
    ... Near the beginning of the document "Unifying types and classes in ... Python 2.2" by GvR, ... The exception would be when we expect that the requested ... have thought that the overhead to do a key lookup is quite a bit less ...
    (comp.lang.python)
  • Re: Exceptions
    ... Are you assuming current rules for guessing 10% exception handling, ... programmers. ... packge Subsys is ... return T raise Oops; ...
    (comp.lang.ada)