2

A prior question had an answer that interested me from a design perspective. I work in geospatial data and occasionally have to deal with voids when I read these binary files into my product. What is the best way to deal with voids when reading them in - throw an exception when you encounter them or return immediately. I believe one of the answers that I read talked of returning rather than throwing an exception. Currently I return rather than throw an exception. But I was wondering whether that is the best approach.

  • Which language do you use? The right answer depends a lot on the features of the language, particularly its type system (if it has one). – itsbruce Jan 26 '15 at 11:55
  • @itsbruce - java – gansub Jan 26 '15 at 11:56
4

When deciding between throwing an exception or returning an error-code, there are a few considerations to take into account.

The primary one is how exceptional is the situation. Is receiving data with voids in them something that should not happen (and an indication that somewhere else something went really wrong), or is that something that your algorithm is supposed to be able to deal with gracefully. If it should not happen, then that is a strong indicator for using an exception.

A second consideration that I often use (and opinions are divided if it is a valid consideration) is if the immediate caller can be expected to resolve the problem. If I know beforehand that the immediate caller is unlikely to be able to do anything about an error I signal, except to pass the buck up the call chain, then I am more inclined to use an exception to report the error because it is less error-prone to let an exception go up the call chain than to report an error-code up several levels.

  • thank you for the answer. Data with voids is actually that can happen in geospatial area. For e.g. mountainous areas can cause shadows preventing data from being observed. So voids are defintiely NOT something gone wrong. – gansub Jan 26 '15 at 12:14
  • 1
    @gansub: Even if voids are normal in geospatial data, it could be an error to pass them on to certain algorithms (like an algorithm to calculate surface area). The error might be that a higher layer didn't do a good job of filtering the voids out. – Bart van Ingen Schenau Jan 26 '15 at 12:24
  • 1
    +1. That said, there's no need to choose; you can always provide both and let the caller decide the most appropriate mechanism. – Doval Jan 26 '15 at 12:30
  • @Doval - you mean have two code sets and let the caller decide which one to call ? – gansub Jan 26 '15 at 12:32
  • @gansub Yes. For example, to turn a string into an integer, C# provides both a parse function that throws an exception when it fails, and a tryParse function that returns a boolean instead. – Doval Jan 26 '15 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.