| Author |
Not declaring the Exception
|
M Rama
Ranch Hand
Joined: Mar 04, 2005
Posts: 88
|
|
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
Joined: Mar 10, 2005
Posts: 77
|
|
|
the code wont compile as MyException3 does not belong to the exception hierarchy.
|
RB
|
 |
Parameswaran Thangavel
Ranch Hand
Joined: Mar 01, 2005
Posts: 485
|
|
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
Joined: May 28, 2004
Posts: 333
|
|
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
Joined: Oct 11, 2004
Posts: 3829
|
|
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
Joined: Mar 01, 2005
Posts: 485
|
|
hi can i know what the role Exception Hierarchy is playing in throwing an Exception
|
 |
M Rama
Ranch Hand
Joined: Mar 04, 2005
Posts: 88
|
|
|
Thanks Rathi Ji. That was the answer I was hoping to hear.
|
 |
 |
|
|
subject: Not declaring the Exception
|
|
|