• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Not declaring the Exception

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from Whizlabs exam




The answer is "Does not compile" and the reason given is, System.out.println("End"); is not reachable. Now

This question does not have the declaration:



If the System.out.println("End"); were not there in the above code, can I say, it won't compile 'cos the MyException3 has not be declared, or would I have to imagine that the exception has been declared somewhere?

I mean, could they test us on something like this?
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the code wont compile as MyException3 does not belong to the exception hierarchy.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ragul
i didn't get the point of Exception Hierarchy in connection with Throwing the method.can u explain me more.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To start with, I think the code you have posted is incomplete. If it is not incomplete, then Rahuls' answer is spot-on.
I guess u forgot to add the following to the code snippet:
class MyException3 extends Exception
{
}
If this is the case, then the explanation is as follows:

The finally block throws an exception which is not caught. The finally block will always be executed. So, the statement System.out.println("End"); is always unreachable, since the exception is not caught and execution terminates abruptly.

Comment out the throw statement in the finally block and it won't throw a compile-time error.
[ April 09, 2005: Message edited by: Kedar Dravid ]
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If the System.out.println("End"); were not there in the above code, can I say, it won't compile 'cos the MyException3 has not be declared, or would I have to imagine that the exception has been declared somewhere?

I mean, could they test us on something like this?



In the exam , you will get clear-cut questions . You don't need to assume anything untill they don't tell ...
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
can i know what the role Exception Hierarchy is playing in throwing an Exception
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rathi Ji. That was the answer I was hoping to hear.
reply
    Bookmark Topic Watch Topic
  • New Topic