Exception Errors

Exception Errors

By Will Watts

Overload, 6(29):, December 1998


Dear John, I just received Overload 28, and am as usual baffled by The Harpist. This time, however, I think it may be his or your fault as well as mine...

On P20 column 1, the Harpist begins a paragraph 'The last of these mechanisms', at which point one's eyes flick up to the paragraph above, to find a list - the last item is calling exit(). I think actually the Harpist means that returning an error code is not possible in destructors - but he definitely got me scratching my head.

In column 2 of P20 he declares a constructor like this

CanFail() : failed(true)
{
  failed = true;
}

Which doesn't strike me as very handy either. I suppose he meant

CanFail() : failed(true)
{
  failed = false;
}

but even this seems a perversely negative and unclear way of writing something like

bool builtFlag;

CanFail() : builtFlag(false)
{
  // body - maybe can exit here...
  builtFlag = true; 
  // successful construction
}

at which point built() becomes a straight accessor function rather than a strange little negator. Or have I missed the point entirely? I am a Delphi-head - there, I've admitted it. When Borland introduced exception handling into its Object Pascal implementation, the result was cleaner, more natural and safer code. Since Stroustrup introduced exceptions into C++, my impression is that resulting C++ code is usually a hideous, hard-to-read mess full of memory leaks. An impression strongly reinforced by the wrapped up function at the bottom of page 21 column 2.

The secret of good exception based code in Delphi is that you very, very rarely need to use the 'except' keyword (approximately analogous to C++'s 'catch') - see the excellent book 'Delphi Component Design' by Danny Thorpe for a wonderful discussion of how Delphi exceptions work.

What the secret of good exception-based code is in C++ I don't know, but I'm sure this ain't it.






Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED


By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED



Settings can be changed at any time from the Cookie Policy page.