This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ExamLab Exception

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, This is from ExamLab.
 
Matthew Raw
Ranch Hand
Posts: 76
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic