Search code examples
javaexceptioncomparable

java "not comparable" exception?


I'm making a custom class that implements comparable, and I'd like to throw some kind of exception if somebody tries to compare two objects that are not comparable by my definition. Is there a suitable exception already in the API, or do I need to make my own?


Solution

  • Not that I know of.

    The most accurate Exception to represent this is probably an IllegalArgumentException: http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html

    You should probably also be implementing Comparable<CustomClass> which will prevent callers from providing an instance of the wrong class.