Feeds

Catch as catch can

A light-hearted look at exception handling

Secure remote control for conventional and virtual desktops

When should you raise an exception?

Glad you asked me that.

You should only raise an exception when you have encountered a truly exceptional situation. You should not raise an exception in a mildly-surprising-for-the-time-of-year kind of situation, and you certainly mustn’t raise an exception on a whim.

For example, if your routine threw because part of the GUID number you have just generated reads ‘SHELLOIL’ if you look at it upside down, this would be a Bad Thing. So please don’t.

As for using exceptions as a spectacular alternative to the much-maligned goto: this is the sort of behind-the-bikesheds activity in which Reg readers would never indulge.

Exception specifications

A quick reminder; exception specifications are extra bits glued onto function definitions that look like this:

int foo() throw(excpt1, excpt2); // can throw excpt1 and excpt2
int moo() throw(nothing);        // can throw nothing
int poo() throw();               // can't throw nothing. Umm...
int coo();                       // can't never not throw nothing

So that’s clear.

Exception specifications divide the C++ world into three rival and irreconcilable schools of thought:

  • There are those who have never heard of them. This faction doesn’t use exception specifications in its code.
  • There are those who have heard of them, but can’t be bothered to put them in. After all, we already gave in on the const thing with parameters, and that’s caused no end of trouble. This faction doesn’t use exception specifications in its code.
  • There are those clever clogs who have actually looked into the matter and given it some thought. These persons have discovered that they aren’t, in fact, any cop. This faction doesn’t use exception specifications in its code.

Until these three groups bury the hatchet and settle their differences, I can see no long-term future for C++.

Meanwhile, Java has a version of exception specifications called “checked exceptions” which are possibly even worse.

What to do if you get an exception

Faced with this question, some writers indulge in hand waving. Fortunately I am able to offer specific guidelines, based on standard practice as observed in commercial software, and some informed guesswork.

  • If you are running as some sort of web service, the standard approach on handling an exception is to send the user a page of ODBC diagnostics, preferably mashed up with a few suggestions from Apache.

    But this approach doesn’t just work with HTML output. For example some banks have also adopted it, as a novel method of telling you that you aren’t going to see your card back from the ATM any time soon.

  • If you are running as a background process with no user interaction, don’t just disappear silently. Be sure to do a memory dump, so gifting the user a digital turd — a binary lump of disk space of no use to man or beast. Naturally it won’t help you-the-programmer find the cause of the exception, because even if the user troubles to send it to you, like everyone else you don’t keep your debug symbol tables in version control, kidding yourself that you can rebuild them identically from the source.

    (By the way, the Windows API call to create this thing, MiniDumpWriteDump(), is one of my faves: as it says in the docs, sometimes you have to throw another exception to get it to fire. Neat.)

  • If you are running as a GUI program, now’s the time to pop up a modal message box. It doesn’t really matter what text you put in it, because the user will ignore it.

    A refinement, especially popular with Delphi programmers, is to put up further, identical message boxes at a one half second interval, so that unless the user intervenes and starts closing them at a greater rate, the whole system will eventually die from memory exhaustion.

  • If you’re running under Windows XP, consider converting the exception into a null pointer dereference in the catch handler:

    catch(...) { // now we're really stuffed
        int * p = 0;
        *p = 22;
    }
    

    This has the advantage over an ordinary crash that you will get one of those special OS-supplied dialogs, that asks permission to send log details back to Microsoft. Naïve users will interpret this as a Windows fault, and will direct their bile Redmondwards.

Of course, all the above techniques can be combined in fresh and original ways. Never be afraid to experiment.

The primary duty of an exception handler is to get the error out of the lap of the programmer and into the surprised face of the user. Provided you keep this cardinal rule in mind, you can’t go far wrong. ®

Security for virtualized datacentres

More from The Register

next story
PEAK APPLE: iOS 8 is least popular Cupertino mobile OS in all of HUMAN HISTORY
'Nerd release' finally staggers past 50 per cent adoption
Microsoft to bake Skype into IE, without plugins
Redmond thinks the Object Real-Time Communications API for WebRTC is ready to roll
Microsoft promises Windows 10 will mean two-factor auth for all
Sneak peek at security features Redmond's baking into new OS
Mozilla: Spidermonkey ATE Apple's JavaScriptCore, THRASHED Google V8
Moz man claims the win on rivals' own benchmarks
FTDI yanks chip-bricking driver from Windows Update, vows to fight on
Next driver to battle fake chips with 'non-invasive' methods
DEATH by PowerPoint: Microsoft warns of 0-day attack hidden in slides
Might put out patch in update, might chuck it out sooner
Ubuntu 14.10 tries pulling a Steve Ballmer on cloudy offerings
Oi, Windows, centOS and openSUSE – behave, we're all friends here
Was ist das? Eine neue Suse Linux Enterprise? Ausgezeichnet!
Version 12 first major-number Suse release since 2009
prev story

Whitepapers

Why and how to choose the right cloud vendor
The benefits of cloud-based storage in your processes. Eliminate onsite, disk-based backup and archiving in favor of cloud-based data protection.
Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Storage capacity and performance optimization at Mizuno USA
Mizuno USA turn to Tegile storage technology to solve both their SAN and backup issues.
Simplify SSL certificate management across the enterprise
Simple steps to take control of SSL across the enterprise, and recommendations for a management platform for full visibility and single-point of control for these Certificates.