aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions 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 "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
Hi all,
In the mock question below why only answers c,d are right
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();
}
}
I mean why b,e are illegal, does a method cann't throw two exceptions at the same time and in the same statement.
And why e is wrong. It can only explain that i only need to throw runtimeexception.
Please reply urgently.
Nisheeth
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hey your hierarquy is faulty.
This is the real one and you can't change it, only add more subclasses.

+ java.lang.Throwable
|
+ java.lang.Exception
|
+ java.lang.RuntimeException
I mean if the last exception is RuntimeException its supertypes must be the ones shown.
Please use http://www.javaranch.com/ubb/ubbcode.html to properly post example programs. Thanks


SCJP2. Please Indent your code using UBB Code
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
You only have to handle checked exceptions(all subclasses of exception except runtime exception) in your code. You don't have to handle Runtime Exceptions at all..
Anybody correct me if I am wrong

Originally posted by Nisheeth Kaushal:
Hi all,
In the mock question below why only answers c,d are right
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();
}
}
I mean why b,e are illegal, does a method cann't throw two exceptions at the same time and in the same statement.
And why e is wrong. It can only explain that i only need to throw runtimeexception.
Please reply urgently.
Nisheeth

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

yes roopa, you are correct. May be on your choice of word...I think when we say an exception is handled, doesn't it mean a problem is taken care of?... Here what you are doing is "declaring" that the method might throws an exception...you are telling the calling method "I am telling you this method you are calling may throw an exception...but it is not my problem...you do what you want with it..."
I think an exception is said to be handled when it is put in a try, catch, finally (optional) statement and is given a lasting solution.
Roopa, everyone...correct me if I am wrong.
Girmay nagesh

Originally posted by Roopa Bagur:
You only have to handle checked exceptions(all subclasses of exception except runtime exception) in your code. You don't have to handle Runtime Exceptions at all..
Anybody correct me if I am wrong


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions
 
Similar Threads
Help me friends. I'll appreciate your answer.(Exceptions)
throws RuntimeException
Reg. Exception : Valiverus' exam
Exception
Throwing exceptions... mock question doubt