Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Throwing exceptions... mock question doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Throwing exceptions... mock question doubt" Watch "Throwing exceptions... mock question doubt" New topic
Author

Throwing exceptions... mock question doubt

Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33
Hi friends,

please check out this question...

Given that method aMethod() throws BaseException, SubException and RuntimeException of the following exception hierarchy







Which of the following are legal

A.public class MyClass {
public void myMethod(){
aMethod();
}
}
B.public class MyClass{
public void myMethod() throws BaseException,RuntimeException{
aMethod();
}
}

C.public class MyClass{
public void myMethod() throws BaseException{
aMethod();
}
}

D.public class MyClass{
public void myMethod() throws Exception{
aMethod();
}
}

E.public class MyClass{
public void myMethod() throws RuntimeException {
aMethod();
}
}

Answers: C and D

what i dont understand is why option B is incorrect?
the quesion states "method aMethod() throws BaseException, SubException and RuntimeException ", then why cant we throw both BaseException and RunTimeException together from myMethod... the way we handle these exceptions is beyond the scope of this question right?can't myMethod choose to throw these exceptions? i know i'm missing something here.. can anyone explain???

thanks in advance
[ September 04, 2007: Message edited by: Radha Kamesh ]
mittu sinha
Greenhorn

Joined: Sep 04, 2007
Posts: 2
RuntimeException are unchecked kind of Exceptions.This kind of exception is managed by JVM itself,Compiler doesnot put any obligation to throw or handle these kind of exception,However All exceptions which are derived from non - RuntimeException class are Checked Exceptions,For this we have to handle and throw these exceptions.
To get more on this refer to K&B Chap-5.

Regards
mittu
Devesh Chanchlani
Greenhorn

Joined: Sep 04, 2007
Posts: 19
I guess the option B may not be correct as the Exceptions need to be thrown in the order of hierarchy, with the base exception coming last....
Jigar Upadhyay
Ranch Hand

Joined: Jan 25, 2005
Posts: 32
This is because inheritance hierarchy. BaseException is the super class of RuntimeException and as per the inheritance rule super class can refer to any of the subclass without casting.

So here "BaseException" or "Exception" can handle any other exceptions and there is no need to define "RuntimeException" to create duplicacy. Here "RuntimeException" is nothing but the "BaseException" for compiler.
Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33
sorry friends, the diagram for the exception hierarchy i had posted came out wrongly.. i have now out it in a code block for the structure to be retained......

according to the diagram, now both BaseException and RuntimeException derive directly from Exception...

i know that Runtime exception need not be declared... but is it wrong to do
so in this case?
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
I wrote code like



It gives me no error.

Try it by your self, You will clear.


SCJP-1.5<br />SCWCD-1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing exceptions... mock question doubt