This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not declaring the Exception

 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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?
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code wont compile as MyException3 does not belong to the exception hierarchy.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • 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.
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • 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 ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Rathi Ji. That was the answer I was hoping to hear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic