Designing exceptions for conversion failures

Posted by Mr.C64 on Programmers See other posts from Programmers or by Mr.C64
Published on 2012-10-31T11:05:44Z Indexed on 2012/10/31 11:13 UTC
Read the original article Hit count: 264

Filed under:
|

Suppose there are some methods to convert from "X" to "Y" and vice versa; the conversion may fail in some cases, and exceptions are used to signal conversion errors in those cases.

Which would be the best option for defining exception classes in this context?

  1. A single XYConversionException class, with an attribute (e.g. an enum) specifying the direction of the conversion (e.g. ConversionFromXToY, ConversionFromYToX).
  2. A XYConversionException class, with two derived classes ConversionFromXToYException and ConversionFromYToXException.
  3. ConversionFromXToYException and ConversionFromYToXException classes without a common base class.

© Programmers or respective owner

Related posts about design

Related posts about exceptions