This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ExamLab Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ExamLab Exception" Watch "ExamLab Exception" New topic
Author

ExamLab Exception

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, This is from ExamLab.
Matthew Raw
Ranch Hand

Joined: Nov 30, 2010
Posts: 76

Hi Saibabaa,
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

The output of this program will be true.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Saibaba

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
i.e. Throwable


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
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Saibabaa Pragada wrote:

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:


Output is:

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:
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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()!
 
GeeCON Prague 2014
 
subject: ExamLab Exception