• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

ExamLab Exception

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, This is from ExamLab.
 
Ranch Hand
Posts: 76
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
 
reply
    Bookmark Topic Watch Topic
  • New Topic