• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throwing exceptions... mock question doubt

 
Radha Kamesh
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote code like



It gives me no error.

Try it by your self, You will clear.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic