Originally posted by Angela Narain:
This is regarding exception hierarchy while calling methods which throws some exception :
Suppose a method throws a checked exception :
[b]
class SubException extends Exception {}
public void aMethod() throws SubException {
throw new SubException();
}
1. With reference to the method "aMethod()" what do i need to declare
in the throws clause of this method.
a) The SubException itself like shown in the example above
b) Super class of SubException in this case Exception.
2. Suppose this method is called by some code ,
In the calling code/method it will be needed that <either>:
a. Declare the exception in the throws clause
b. Handle the exception in a try-catch block
So what exceptions possibly i can declare :
If case a. : ?
If case b. : ?
3. Suppose in the calling method if i declare
in the throws clause as well as use try-catch block
So what exceptions possibly i can declare :
If case a. : ?
If case b. : ?
4. What is the exact sequence of the order of exceptions
that can be placed in the throws clause as well
in the try-catch block of the calling method ?
I am not able to get the exact sequence of the hierarchy
of passing on the exceptions / subexceptions to the calling method .
Kindly correct if anything above is wrong.
[/B]
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Originally posted by Angela Narain:
1. With reference to the method "aMethod()" what do i need to declare
in the throws clause of this method.
a) The SubException itself like shown in the example above
b) Super class of SubException in this case Exception.
ANS : 1. Either a or b
I think , it should be both a and b
and not either .
Pls. correct me if wrong.
Originally posted by Angela Narain:
[..]
Specifically I wanted to know which exceptions ( SubException or Exception )is to be put in the throws clause or it try-catch used which possible exceptions ( SubException or Exception ) to be put for each case
Something like the below for points 2,3,4
if case a : SubException or Exception
if case b : Only Exception
Originally posted by Angela Narain:
[..]Point 1:
If I catch let's say "SubException" using the try-catch block in the calling code ( going towards catching the specific exception as per previous explanation ) and also i declare "Exception" in the throws clause,then what is the use of declaring the Exception class in throws clause ? I suppose it is not required.[..]
Originally posted by Angela Narain:
[..]Point 2:
What all exceptions i can declare in the catch of the calling method,if i declare some exception class in the throws clause also.
Is it required to be subclass of the one declared in the throws method.[..]
Consider Paul's rocket mass heater. |