ClassCastException generally creeps in when you try to cast an object of one type into another incompatible type. For e.g.
When you say
String s = (String)(an object of StringBuffer);
The above will throw a
ClassCastException.
But if an object is a type of the casted object, then it doesn't throw this exception.
e.g.
// this will not throw the exception
Object o = (Object)(any kind of object);
To avoid this kind of Exception, you may use the instanceOf operator to check whether your object is castable into a particular type.
Sid
[ May 16, 2007: Message edited by: Sidd Kulk ]