Skip to content
December 2, 2009 / lawrencebarsanti

Exceptions: to throw or not to throw…

Exceptions are a useful programming concept because they1:

  1. separate error handling code from regular logic
  2. propagate errors up the stack automatically
  3. provide a convenient way to group and handle different types of errors

I have been thinking about exceptions a lot lately and I would have to agree with the commonly help opinion that exceptions are a useful programming concept.  However, for all the good they provide, I do think that there is one major disadvantage and that it does not get much attention.

it is hard to use exceptions and exception handling correctly

By this I mean it is hard to know when an exception is the right way to communicate an error condition.  I have read several blogs recently and it seems that most people subscribe to one of the following philosophies:

  1. exceptions should be used whenever a function cannot perform its job
  2. exceptions should only be used for unpredictable events

To help clarify the difference between the two trains of thought, lets consider a function that converts an ascii string to an integer.  These two groups disagree on what should happed when the function is passed the string ‘abc’.  People in group 1 want an exception while people in group 2 do not.  However, both groups would agree that an exception should be thrown if the function is passed a null pointer or something along those lines.

Both camps have some good arguments explaining why their approach is better [here are a few 1, 2, 3, 4].  Since I do most of my coding in Delphi, I decided to look to the choices made by its designers for guidance.  Here’s what I found2:

//throws an exception if S cannot be converted to an integer
function StrToInt(S: String): integer;
// returns false if S cannot be converted to an integer
function TryStrToInt(S: String; out Value: Integer): boolean;
//returns Default if S cannot be converted to an integer
function StrToIntDef(S: String; Default: Integer): integer;

Hmmm… why didn’t they pick a side?  I believe that the designers realized that there is no best approach because both approaches can be useful depending on the situation.  So, if you are writing a really generic function, like one that converts a string to an integer, it is probably best to provide both options but if your writing a really specific function, like one that reads an applications settings, just do what is best for the code that will be calling your functions.

  1. http://java.sun.com/docs/books/tutorial/essential/exceptions/advantages.html
  2. dig a little deeper and you will discovered that these functions are just wrappers for the Val function which does not use exceptions
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: