14

What type of Exception should I throw if the wrong type of object is passed into my compareTo method?

ClassCastException?

ritch
  • 1,760
  • 14
  • 37
  • 65
  • Can you show us your `compareTo` code? – adarshr May 03 '12 at 11:39
  • 2
    Well, that depends on how you would handle that exception. I'd probably throw an `IllegalArgumentException` with a proper message. Btw, might there be a way to not allow passing the wrong type of object? Could you elaborate on the use case? Maybe we could help you prevent that runtime check and use the power of the compiler. Note that `Comparable` can be paremeterized with a generic type parametera thus `compareTo` can use specific parameters. – Thomas May 03 '12 at 11:41
  • 1
    If you are downcasting in the implementation of compareTo, you'll get that CCE for free. Why bother throwing it explicitly. – Marko Topolnik May 03 '12 at 11:47

2 Answers2

22

It would be IllegalArgumentException in a general sense when the passed in value is not the right one.

However, as @Tom's answer below suggests, it could also be a ClassCastException for incorrect types. However, I am yet to encounter user code that does this.

But more fundamentally, if you're using the compareTo with generics, it will be a compile time error.

Consider this:

public class Person implements Comparable<Person> {
    private String name;
    private int age;

    @Override
    public int compareTo(Person o) {
       return this.name.compareTo(o.name);
    }
}

Where do you see the possibility of a wrong type being passed in the above example?

Community
  • 1
  • 1
adarshr
  • 61,315
  • 23
  • 138
  • 167
  • You seem to disagree with the API docs which are keen on `ClassCastException`. – Tom Hawtin - tackline May 03 '12 at 11:48
  • Well, looks like the OP has more fundamental problems. All this should be handled at compile-time. Will factor your point in to my answer. – adarshr May 03 '12 at 11:49
  • 1
    @TomHawtin-tackline if adarshr was Chuck Norris, then the Javadoc would've been wrong :) – Marko Topolnik May 03 '12 at 11:49
  • 1
    I would consider the question wrong. The answer definitely leads the OP in the right direction :) – adarshr May 03 '12 at 11:52
  • I suspect the Javadoc was written before generics were considered. – Peter Lawrey May 03 '12 at 11:56
  • 1
    If you do `Comparble p = new Person(); p.compareTo(1); // throws a ClassCastException` Using generics is likely to mean you don't need to throw ClassCastException explicitly, even better the compiler has a chance to report an error at compile time. – Peter Lawrey May 03 '12 at 12:00
7

Unsurprisingly, the API docs specify the exception to be thrown in this case.

ClassCastException - if the specified object's type prevents it from being compared to this object.

Assuming you are using generics, you will automatically get this exception if someone attempts to call your methods using raw types, reflections or some other unsafe technique.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • I disagree with this as `ClassCastException` will be thrown by the JVM, where the OP is asking about an exception _they_ need to throw. Therefore I favour the `IllegalArgumentException` answer. Perhaps it's a language thing, however. – maksimov May 03 '12 at 11:53
  • 1
    @maksimov The API docs are very clear. You are disagreeing with the specification of the method, which is an unusual position to take. – Tom Hawtin - tackline May 03 '12 at 13:43
  • Tom, I just realised I must be blind. Somehow I managed to filter out the `compareTo` they are implementing. I'd like to change my statement above now :-) – maksimov May 03 '12 at 13:49
  • +1 this is the best answer for non-generic implementations and it gives the lazy ones among us a straight link to the API docs to boot! – Stijn de Witt Feb 11 '13 at 15:33