Catch as catch can

A light-hearted look at exception handling

Stob Exception handling is a comparative newcomer to the programmer’s toolset.

The mighty for loop, the enigmatic if statement and the cheeky little counter increment have been with us since the first automatic languages bubbled to the surface of the primordial programming bog at Manchester, more than half a century ago. But, according to my skimpy research, it wasn’t until IBM’s PL/I came along, in the mid 1970s, that exception handling appeared in a language.

PL/I’s exceptions were based on a proposal by John B. Goodenough. Goodenough was strongly motivated in his design, I believe, by the behaviour of his colleagues every time one of his programs crashed. I suggest they were in the habit of jeering — “Never mind John, that’s good enough” — and then falling about laughing. Truly is it said that ambition and bullying are the twin fuels that drive the engine of progress.

After PL/I, the next mainstream-ish language to support exceptions was Ada. Like PL/I, Ada was confidently expected to have a glorious future. At one time, the US Department of Defense mandated the use of Ada upon all its suppliers for all safety-critical work. Well, all those suppliers who carelessly failed to use the magic phrase ‘but it will be much cheaper if we do it in C’.

To the surprise of their powerful backers, neither PL/I nor Ada really “took on”. Despite guest appearances in exotica such as Clu and Modula-3, it wasn’t until C++ compilers began to support exception-handling constructs, in the early 1990s, that the idea was really adopted by the mainstream.

(Actually, I don’t believe this. I remember doing ON ERROR GOTO stuff in Basic in the early 1980s; if that wasn’t a kind of exception handling, what the Dickens was it? Perhaps the academics take the view that, although it may have been mainstream, it was only Basic and so doesn’t count. Fair enough. Objection withdrawn, m’lud.)

Before we hit the techie nitty-gritty, a touch of etymology. The word “exception”, of course, comes from the Latin irregular verb excepto — conjugates excepto, exceptere, crashli, dumpdum — literally meaning “to encounter unexpectedly something unpleasant that you can’t prevent”. Legend has it that it was coined by a well-known peace consultant in pressing and infamous circumstances: “Et tu Brute? My, that’s an exceptionally sharp dagger.” If you picked up the teeny Carry On reference there, you may now take a biscuit.

Throwy-catchy nitty-gritty

I will discuss exceptions in the context of C++, using it as a baseline to compare with Delphi’s Object Pascal, Java and C#.

I apologise in advance to the industry’s senior commentator for not covering Scheme or other Lisp dialects and, thus, continuing the tendency to dumb down. This does not mean that I fail to recognise that Lisp is still #1 for key algorithmic techniques such as recursion and condescension. It just means that I have no idea how, or indeed if, Lisp handles exceptions.

First, “finally”.

finally is the entirely indispensable keyword that C++ doesn’t have. For the benefit of just-C++ people: a finally clause contains all the inline tidying up that must always happen, whether or not you get an exception in the try block. The path of execution through the code is clear… although there is always the danger that you will accidentally release a resource outside the finally block, or forget it altogether.

C++ says you must wrap your resources up in classes, bung them on the stack, and have the destructor unpick the damage as control bubbles up through scope. C++ says that finally leads to lazy, ad hoc coding practices.

Javaman: Oh yeah, that’s neat. So every time you want to protect some resource or teeny bit of state, you’ve got to jump out of line and knock out a whole class? Probably just to free up a couple of heap-allocated objects, I should think. Yup, that makes sense.

Mr C++: Oooh, hark at the supreme garbage collector! Just because you need a destructor (that’s like a finalize() only it actually gets run some time soon, by the way) you don’t have to make a special class, you know. A generic is not just for a container. Whoops, hush my mouth, in your case it is.

Javaman: Here we go. Here we go. Do tell, are you planning on using auto_ptr, and maybe munching an array that needs to be delete[]ed? And after that let’s talk about Total Cost of Ownership…

Charlie C#: Hold up, Java. Too much finally really can get up your nose. There’s a lot to be said for implementing a bit of an IDisposable class, push a more code out of the app and into the nice, pre-tested library.

Denis Delphi: That’s right, the heterogeneous approach is best. Use finally for ordinary objects, but do semi-hidden reference counting for the contents of strings and objects that are cast to interfaces. That’s the way to do it. Unless we are running under .NET, of course, in which case we…

Everybody: Oh shut up, you great dork.

Thus it may be seen that there are many clever and compelling arguments to be made on all sides.

Other stories you might like

  • NASA installs a new and improved algorithm to better track near-Earth asteroids

    Nearly 20 year-old software used to protect humanity gets an upgrade

    NASA has upgraded its near-Earth asteroid monitoring algorithm to model hazardous space rocks more accurately after nearly two decades, it announced on Tuesday.

    The new system, dubbed Sentry-II, is more powerful than its predecessor, Sentry. Astronomers working at the space agency's Center for Near Earth Object Studies can now automatically calculate thermal influences that nudge an asteroid’s orbit, potentially sending it hurtling towards our home planet.

    The so-called Yarkovsky effect describes the subtle and gradual change of motion when asteroids are heated by the Sun’s light. When asteroids spin, one side of its surface exposed to the star gets heated. As it continues to rotate, the hot region enters shade and cools down. Infrared energy is radiated outwards; the photons carry momentum and impart a tiny thrust on the asteroid. Over long periods of time, these small kicks can change their paths and knock them out of their original orbit.

    Continue reading
  • Facebook slapped with an eyepopping $150B lawsuit for spreading hate speech against Rohingya refugees

    Lawsuit claims social media giant's algos helped Myanmar military crackdown on the Rohingya

    Meta was sued on Tuesday for a whopping $150 billion in a class-action lawsuit for allegedly amplifying hate speech and aiding the Myanmar military in the genocide of the Rohingya people.

    The case, led by an anonymous Rohingya refugee living in the US, accuses the entity formerly known as Facebook of inciting hatred and inflicting real harm on the predominantly Muslim group for years. Not only did the social media platform ignore hate speech posts, it's alleged that the service's algorithms actively promoted anti-Rohingya propaganda as hundreds of thousands of people fled from Myanmar to escape persecution.

    Facebook has already acknowledged its role in the campaign, which saw an estimated 25,000 people perish and 700,000 forced from the country. The lawsuit also comes after ex-employee and whistleblower Frances Haugen leaked internal documents demonstrating how its algorithms prioritized engagement over safety.

    Continue reading
  • Power management IC shortage holding cars, laptops, hostage

    Couple of cents-worth of kit causing big problems for the year to come

    The shortage of power management chips is worsening and holding back companies from building cars, PCs and items with batteries or an on-off switch, Trendforce said in a study this week.

    Power management ICs cost just a few cents, and are among cheap chips that include display driver and USB-C components that are in short supply. These chips are as important to PCs and other electronics as CPUs or memory.

    The demand for PMICs has gone through the roof with the emergence of electric cars and growing demand for PCs and consumer electronics during the past 20 plus months. Trendforce expects the prices will go up by 10 per cent to a six-year high of $0.23.

    Continue reading

Biting the hand that feeds IT © 1998–2021