• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
prasanna perera
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!
This question is from Valiveru's mock exam
Question 37.
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();
}
}
given ans: c,d
I think b is also correct ?
 
Harry Singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think only D is correct. You really dont have to declare a throws for runtime exceptions.
B does not throw SubException so that would be a compile time error since it is a checked excetion.
Same reasoning for C too.
Can anyone back me up ?
Thanks
 
Dev Prakash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also think that b,c,d are correct. Because in option b it throws BaseException which is the Parent of SubException.
 
Bin Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried the code.
yes, b,c and d are all right.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic