Expection handling

From Wikipedia, the free encyclopedia

Expection Handling is a computer programming Anti-pattern. It refers to using a computer language's error handling structures to perform normal program logic, like control flow. The term comes from combining exception handling with the word expect: In other words, expection handling is catching occurrences that you expect to happen.

The "reference implementation" of expection handling is blindly stepping through a collection, and catching an error when you go beyond the bounds of that collection.

try
{
  int idx = 0;
  while (true)
  {
    displayProductInfo(prodnums[idx]);
    idx++;
  }
}
catch (IndexOutOfBoundException ex)
{
  // nil
}
 
}

This anti-pattern can cause a number of problems. Firstly, error handling is often slow, as it is meant to be correct, and rarely used. Secondly, you cannot be sure that the exception that occurs is the one you predicted, and is not caused by other code, such as an error within the "displayProductInfo()" subroutine above. This can lead to subtle logic errors.