Class IllegalClassException
- All Implemented Interfaces:
Serializable
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
}
- Since:
- 2.0
- Version:
- $Id: IllegalClassException.java 905636 2010-02-02 14:03:32Z niallp $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final longRequired for serialization support. -
Constructor Summary
ConstructorsConstructorDescriptionIllegalClassException(Class expected, Class actual) Instantiates with the expected and actual types.IllegalClassException(Class expected, Object actual) Instantiates with the expected type, and actual object.IllegalClassException(String message) Instantiates with the specified message. -
Method Summary
Modifier and TypeMethodDescriptionprivate static final StringsafeGetClassName(Class cls) Returns the class name ornullif the class isnull.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDRequired for serialization support.- See Also:
-
-
Constructor Details
-
IllegalClassException
Instantiates with the expected type, and actual object.
- Parameters:
expected- the expected typeactual- the actual object- Since:
- 2.1
-
IllegalClassException
Instantiates with the expected and actual types.
- Parameters:
expected- the expected typeactual- the actual type
-
IllegalClassException
Instantiates with the specified message.
- Parameters:
message- the exception message
-
-
Method Details
-
safeGetClassName
Returns the class name or
nullif the class isnull.- Parameters:
cls- aClass- Returns:
- the name of
cls, ornullif ifclsisnull.
-