Image for post
Image for post
Photo by Kelly Sikkema on Unsplash

The Throw Keyword was a Mistake

Try and catch are great. Throw is a mess.

Chris Fox
Chris Fox
Dec 20, 2019 · 6 min read

Exception Handling

Image for post
Image for post

This meant that something horrible had happened. Usually the problem was the software tried to read or write some memory outside its address space. It could mean some other problem; a full disk, a device error, but it was an unrecoverable condition, something that software developers call an exception.

There was only one option: tap the spacebar or use a mouse, and invoke “OK.”

Why? It’s not OK. You may have just lost two hours of work (that’s your fault. Hit Ctrl-S every few seconds or you deserve to lose it). But this still sucked.

Exceptions aren’t new. Dealing with them in a more elegant way than inelegantly crashing goes all the way back to 1962 in a near-forgotten language called LISP. See the Resources section for an article on the history.

Languages in modern programming pulled the better ideas from the diverse implementations of handling exceptions and formed a simple language syntax with a few new keywords, and Structured Exception Handling (SEH) was born.

The idea was that software developers could trap exceptions and offer a more elegant shutdown than simply crashing and losing everything:

catch(Exception exception)

So far so good. Unless the application data was corrupted by the Horrible Event you had a cleaner shutdown and could restart the program and continue.

And it got better. Not all exceptions meant shutting down; some were recoverable. If the program tried to access forbidden memory it could return to the previous state and continue. Happy days.

Can’t Leave Well Enough Alone


Calling this new keyword would make the instruction pointer jump to the nearest catch block, wherever that may be; exception types were defined so the jump could be to a selected handler. This may have been a well-meant idea, more likely it was just to be orthogonal and provide developers with both ends of the transaction. Wise developers; experienced developers; thoughtful developers. Probably nobody ever thought what would happen when they put this catastrophic operation in the hands of less experienced developers.

And we can’t put the genie back in the bottle.

What’s So Special About Exceptions?

Example. Our intrepid short-attention-span test-driven-developer is three levels deep in void functions and suddenly discovers a potential error condition. He doesn’t want to “refactor” three void functions to return error codes, he has a foosball game with the team in five minutes, so he issues a call to throw and wraps the call to the first of the void functions in a try-catch block and runs off to his game. Facepalm.

Throwing exceptions becomes just another tool in the box, a glorified longjmp; why should it be restricted to real exceptions?

Why indeed.

And Then Came Java


Before this, using this sledgehammer of a keyword on a thumbtack of an issue was just bad judgment, Java came along and validated that bad judgment.

The Year Everything Crashed

Everyone was using throw everywhere, and it wasn’t working right. Garbage Collection was far from perfect, far from reliable, and developers using throw just expected things to be magically cleaned up. They weren’t.

The Performance Penalty

Processors and RAM have had to stay well ahead of the software running on them because too many developers write inefficient code. New languages had to collect allocated memory, cleaning up after sloppy work because hardly anyone had the discipline to properly manage memory (I remember a Microsoft division meeting where a David Stockman clone sagaciously told us that memory leaks were a fact of life). Rather than cultivate discipline the languages had to add toilet paper functionality.

With throw this problem was squared and cubed.

The Real Problem With Throw

Control Flow

Not Anymore

One early large Java project was subject to analysis with advanced tools and found to have an astounding 13,000 ambiguous code paths based on thrown exceptions; the behavior of the code was massively unpredictable.

What we can predict, and with certainty, is that developers are not going to write with the rigor required to use this keyword safely. Software development is disintegrating before our eyes; focus and concentration are deprecated and just as inefficient code is mitigated by ever faster processors, increasingly sloppy work is mitigated by an obsessive shift of focus from development to testing.

People can’t enter a flow state anymore, not when their day starts with a slog of a commute to attend a daily scrum and they are interrupted all day by meetings and open office conversations. It’s not possible to rise above mediocrity when you can’t concentrate and software companies are more concerned with team cohesion than with quality work.

And it’s in this world that we have this horrible keyword with its wholly unpredictable behavior.

What We Can Do

The Impossible Dream

  • the keyword would be removed from all languages that use it
  • only the language runtime and operating system would be allowed to raise exceptions

That isn’t going to happen; most projects would break. They would have to be rewritten.

Within Our Grasp

This preserves the original intent; programs don’t crash so much and recovery is possible. Software development goes back to being a deterministic endeavor and we regain some stability, probably lot of stability.

Don’t use throw. Just stop. Stop now.


The Startup

Medium's largest active publication, followed by +670K people. Follow to join our community.

Sign up for Top Stories

By The Startup

A newsletter that delivers The Startup's most popular stories to your inbox once a month. Take a look

Create a free Medium account to get Top Stories in your inbox.

Chris Fox

Written by

Chris Fox

American Software Engineer living in Vietnam. Classical musician (guitar, woodwinds), weightlifter, multilingual, misanthrope • XY

The Startup

Medium's largest active publication, followed by +670K people. Follow to join our community.

Chris Fox

Written by

Chris Fox

American Software Engineer living in Vietnam. Classical musician (guitar, woodwinds), weightlifter, multilingual, misanthrope • XY

The Startup

Medium's largest active publication, followed by +670K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store