15.01.2013 Views

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8 - Back to Basics: Exceptions<br />

Back to Basics: Exceptions<br />

FAIL FAST - JIM SHORE<br />

E<br />

xceptions are such powerful constructs that developers can get a little overwhelmed and far too<br />

defensive when dealing with them. This is unfortunate because exceptions actually represent a<br />

key opportunity for developers to make their system considerably more robust. In this chapter<br />

we'll look at three distinct aspects <strong>of</strong> exceptions : handling, creating and throwing them. Since<br />

exceptions are unavoidable you can neither run nor hide, so you might as well leverage.<br />

Handling Exceptions<br />

Your strategy for handling exceptions should consist <strong>of</strong> two golden rules:<br />

1. Only handle exceptions that you can actually do something about, and<br />

2. You can't do anything about the vast majority <strong>of</strong> exceptions<br />

Most new developers do the exact opposite <strong>of</strong> the first rule, and fight hopelessly against the second.<br />

When your application does something deemed exceptionally outside <strong>of</strong> its normal operation the best<br />

thing to do is fail it right then and there. If you don't you won't only lose vital information about your<br />

mystery bug, but you risk placing your application in an unknown state, which can result in far worse<br />

consequences.<br />

Whenever you find yourself writing a try/catch statement, ask yourself if you can actually do something<br />

about a raised exception. If your database goes down, can you actually write code to recover or are you<br />

better <strong>of</strong>f displaying a friendly error message to the user and getting a notification about the problem?<br />

It's hard to accept at first, but sometimes it's just better to crash, log the error and move on. Even for<br />

mission critical systems, if you're making typical use <strong>of</strong> a database, what can you do if it goes down? This<br />

train <strong>of</strong> thought isn't limited to database issues or even just environmental failures, but also your typical<br />

every-day runtime bug . If converting a configuration value to an integer throws a FormatException<br />

does it make sense continuing as if everything's ok? Probably not.<br />

Of course, if you can handle an exception you absolutely ought to - but do make sure to catch only the<br />

type <strong>of</strong> exception you can handle. Catching exceptions and not actually handling them is called<br />

exception swallowing (I prefer to call it wishful thinking) and it's a bad code. A common example I see<br />

has to do with input validation. For example, let's look at how not to handle a categoryId being<br />

passed from the QueryString <strong>of</strong> an ASP.NET page.<br />

int categoryId;<br />

try<br />

{<br />

categoryId = int.Parse(Request.QueryString["categoryId"]);<br />

}<br />

catch(Exception)<br />

<strong>Foundations</strong> <strong>of</strong> <strong>Programming</strong> Copyright © <strong>Karl</strong> <strong>Seguin</strong> www.codebetter.com<br />

64<br />

8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!