File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help me friends. I'll appreciate your answer.(Exceptions) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help me friends. I Watch "Help me friends. I New topic
Author

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

Dmitry Golynkin
Greenhorn

Joined: Aug 13, 2002
Posts: 23
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? ))


good luck
Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
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

Joined: Aug 13, 2002
Posts: 23
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

Joined: Jun 04, 2002
Posts: 133
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me friends. I'll appreciate your answer.(Exceptions)