on line 20 you rethrow the Throwable t which IS-A IllegalMonitorStateException. You upcast it to RuntimeException before rethrowing. Nevertheless, this does not affect the fact, that t still IS-A IllegalMonitorStateException. Therefore, on line 25, the proper catch is invoked.
This concept is the same as with regular objects. For example, you can write
when we are using polymorphism for objects, the object type will never change, just the reference type will change when we create new instance of the class, then due to start() method, object of IllegalMonitorStateException will be created and thrown there
now in the catch blocks we are just referring to the object of type IllegalMonitorStateException using the super type
when the instanceof test results true, you are upcasting it to RuntimeException
still the object is of type IllegalMonitorStateException
hope this helps
have a nice time
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
This doesn't work in the same way as castig primitives (char to byte, byte to int, int to long etc.).
If an object of given class is created, it will stay an object of this class forever - it is not possible to change the class of any object.
You cannot "upcast" the object - it is not possible.
Look at this simple example:
As You see - "upcasting" doesn't work, the class of the object does not change.
If you want to "change" a class of exception "t" to RuntimeException, instead of casting you can wrap the exception t into the RuntimeException in this way:
I would just like to make a personal comment about the example Saibabaa gave. I haven't come across it on ExamLab, but I hope I don't, it has to be one of the most convoluted examples I have experienced, it took me 15 mins to figure out what was going on! Perhaps its just my slow mind Yet goodness, its a humdinger of a poorly written program (in my opinion of course )
be a well encapsulated person, don't expose your privates, unless you public void getWife()!