Author(s)Matthew Hawthorne, Gary Gregory, Stephen Colebourne
Thrown when an object is an instance of an unexpected type (a class or interface).
This exception supplements the standard IllegalArgumentException
by providing a more semantically rich description of the problem.
IllegalClassException represents the case where a method takes
in a genericly typed parameter like Object (typically because it has to due to some
other interface it implements), but this implementation only actually accepts a specific
type, for example String. This exception would be used in place of
IllegalArgumentException, yet it still extends it.
public void foo(Object obj) {
if (obj instanceof String == false) {
throw new IllegalClassException(String.class, obj);
}
// do something with the string
}
Thrown when an object is an instance of an unexpected type (a class or interface). This exception supplements the standard
IllegalArgumentExceptionby providing a more semantically rich description of the problem.IllegalClassExceptionrepresents the case where a method takes in a genericly typed parameter like Object (typically because it has to due to some other interface it implements), but this implementation only actually accepts a specific type, for example String. This exception would be used in place ofIllegalArgumentException, yet it still extends it.public void foo(Object obj) { if (obj instanceof String == false) { throw new IllegalClassException(String.class, obj); } // do something with the string }