What type of Exception should I throw if the wrong type of object is passed into my compareTo method?
ClassCastException?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It would be
IllegalArgumentExceptionin 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
ClassCastExceptionfor incorrect types. However, I am yet to encounter user code that does this.But more fundamentally, if you’re using the
compareTowith generics, it will be a compile time error.Consider this:
Where do you see the possibility of a wrong type being passed in the above example?