[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: C++ exceptions
From:       Michael Pyne <michael.pyne () kdemail ! net>
Date:       2005-12-06 2:29:22
Message-ID: 200512052129.26847.michael.pyne () kdemail ! net
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


On Monday 05 December 2005 21:04, David Johnson wrote:
> On Monday 05 December 2005 05:55 am, R.F. Pels wrote:
> > Not to mention the fact I had to
> > use my airsickness bag when I saw the 'use-return-codes' solution.
>
> Here's the style of code that causes me to reach for that little baggie:

Well you've successfully emulated error return codes with exceptions. :P

Of course nothing is perfect (and especially so with C++ ;).  But exceptions 
can be useful, especially when used as intended.  It is especially useful for 
situations where any error in a long function block is pretty much guaranteed 
to be a fatal logic error (i.e., failure, but no cleanup necessary).

And even if cleanup is necessary, if you properly control the lifetime of the 
objects in question using classes, then even that isn't an issue.

i.e.

bool doStuff(const HTTPData &html)
{
  try {
    // Might throw if this wasn't HTML.
    HTMLDocument doc = html->htmlDocument();

    // Can throw if no such node.
    Node body = doc.findFirstElement("body");

    // Can throw if no such attribute.
    Attribute bgColor = body.getAttribute("bgColor");

    // This probably couldn't throw in a sane impl but you
    // get the point.
    QString clrString = bgColor.asString();
  }
  catch (DOMException e) {
    kdError(DEBUG_AREA) << "Error with request: " << e.error() << endl;

    // Failed, return error.
    return false;
  }

  return true;
}

Notice how I even managed to use both techniques.  However, if I hadn't used 
exception where I did, I would have had to manually check every return code, 
otherwise I would risk having the application crash on malformed network data 
(or worse...).  In this case, exceptions were a boon and not a bane.

But, it is much easier just to ignore return codes, so I shall expect to hear 
whining about exceptions until the very day I die. ;)

Regards,
 - Michael Pyne

[Attachment #5 (application/pgp-signature)]

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic