aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Not declaring the Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Not declaring the Exception" Watch "Not declaring the Exception" New topic
Author

Not declaring the Exception

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
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: 3830

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: 91
Thanks Rathi Ji. That was the answer I was hoping to hear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not declaring the Exception
 
Similar Threads
Casting Throwbale to Exception
Exception
throw in finally clause
Finally
Exceptions...