Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me friends. I'll appreciate your answer.(Exceptions)

 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From list of freeware exams:
Given that method aMethod() throws BaseException, SubException and RuntimeException of the following exception hierarchy
java.lang.Exception
|
+ - - BaseException
|
+ - - SubException
|
+ - - java.lang.RuntimeException
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();
}
}
What exactly does it want from me? ))
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all Exceptions other than those extending RuntimeExceptions are specified as Checked Exceptions . These when thrown from a inside method should be either caught or declared in the throws clause of the method.
So in the question, Exception, BaseException, SubException, are of checked Exceptions. Since none of them are caugfht inside method , they should be in the throws clause.
Again , if a method throws Exception , that will be enough for all the subTypes of Exception.
RuntimeException is supposed to bubble up since they are due to run time error and not due to run time errors . So it is not must to try catch or throw a RuntimeExceptions. But still they can
also be caught ..
see this code
public class Test3 {
public void aMethod(int i) throws Exception, RuntimeException{
if(i==0) throw new RuntimeException();
throw new Exception();
}
public static void main(String args[]){
Test3 t3= new Test3();
for(int i = 0; i<2 ;i++){
try{
t3.aMethod(i);
}catch(RuntimeException e){
System.out.println("RuntimeException");
}
catch(Exception e){
System.out.println("Exception");
}
}
}
}
prints
Exception
RuntimeException
So in the question, D is the answer.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dimyan,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
[ August 14, 2002: Message edited by: Valentin Crettaz ]
 
zarina mohammad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}

B,C,D are correct answers.
B is correct because BaseException can also handle the subclass SubException
C is correct for the same reason.
D is also correct as the Superclass Exception can handle all the subclass exceptions(BaseException and SubException).
 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin Crettaz - As I've understood I have to enter my full name, not a nickname. By the way - your href doesn't exist.
Thanks to:
Binu K Idicula and zarina mohammad
- Mmmm... in what answer I have to trust?
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dmitry,
I think the confusion stems from your exception heirarchy diagram. Maybe you meant something like this:

(Okay, so it's not code, but I can't think of any other way to display this properly )
In this case, B, C, and D are correct. If your original diagram is correct (where all are direct subclasses of Exception, meaning SubException is not a subclass of BaseException), then only D is the correct answer.
Hope this helps,
Paul
[ August 14, 2002: Message edited by: Paul Villangca ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic