A ClassCastException is a runtime Exception. Since the condition in your 'if' statement evaluates to false, the program doesn't make it inside the subsequent block. The cast is never attempted, therefore, no error. Hope this helps!
Joined: Dec 10, 2001
Welcome to JavaRanch, Leo! In your first example, you tried to get something out of TestList and turn it into something it's not. That commented out if statement checks to see if the thing coming out of the TestList is of the right data type before casting it to that data type, that's why the error doesn't occur when the if statement is not commented out. The lesson here should perhaps be, if you have a reference to a Fish object, you cannot cast it into a reference of type Dog (unless the Fish object was actually a Dogfish, of course). (Hey! I don't recall seeing a promise of quality humor 'round these parts - just being friendly to Greenhorns is all.)