• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Reg. Exception hierarchy

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is regarding exception hierarchy while calling methods which throws some exception :
Suppose a method throws a checked exception :

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.

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Exception is the super class here you can just try & catch Exception in all methods that call aMethod()or you can add it to the method in the throws clause. This way you will cover any other exception that might be thrown.
Correct me if I am wrong.
Roopa.

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]


 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,

  1. Either a or b
  2. Either a or b; the calling method can handle the exception in a try/catch or throw it
  3. In both cases you can catch or throw Exception or SubException
  4. The sequence is important in a try/catch block. Always put the most specific exception first.

  5. Hope that helps.
    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2. Suppose this method is called by some code ,
In the calling code/method it will be needed that :
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. : ?
ANS :2. Either a or b; the calling method can handle the exception in a try/catch or throw it
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. : ?
ANS : In both cases you can catch or throw Exception or SubException
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 ?
ANS : 4. The sequence is important in a try/catch block. Always put the most specific exception first.
hi Jane,
Thanks for the reply. To add further to my query :
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


 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anjela,

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.


Since aMethod() is throwing SubException(), declaring SubException in the throws clause would suffice.Alternatively, you could declare a superclass of SubException (Exception) also.However, you certainly donot require both of them.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anjela,

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


Since in this case, you know for sure that aMethod() is throwing
SubException, you should only throw SubException.It is not syntatically incorrect to throw Exception or catch Exception, but this would mean that you are expecting some other exceptions to be thrown apart from the SubException.If you are aware of specifics why do you want to go into the generics?
If you specify Exception in the throws clause, the Developer calling this method would need to handle all the exceptions (using try..catch block) that he thinks would possibly be encountered.This makes things more generic rather than specific.You may not want this to happen, because when you define a method you possibly know about the exceptions it might throw.Infact, this is the reason you created something like SubException in the first place and threw SubException instead of Exception.It makes the Developer's life that much easier!
Depending on the exception thrown, the Developer can use the try..catch block.In this case, again since you have defined the method to throw SubException, you will only try to catch this exception.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anjela,
According to Khalid Mughal,it is not considered good programming practice to specify Exception Superclasses when the actual exceptions thrown in the method are instances of their subclasses. Of course! the compiler will not scream at you for doing so . (Section 5.8, Page 163)
As far as my understanding goes...if one knows the specific subclass exceptions, specifying them alone would suffice.
Correct me if I am wrong!
Hope this helps...
Shyam
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep,
Thanks for the reply. It made my concept on exception handling
quite clearer.
I have still some more doubts as :
<quote>
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 ?
case a. Declare the exception in the throws clause
case b. Handle the exception in a try-catch block
If case a. : ?
If case b. : ?
ANS : In both cases you can catch or throw Exception or SubException
</quote>
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.

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 .

Pls. can you explain the above ?

 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anjela,


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.[..]


Precisely!You donot need to throw Exception that you have already caught for the called method.However, [i]for the calling method, you may still need to declare the exceptions in throws clause.
For example, consider this piece of code snipplet:

In the above code, we have used try..catch block for the called method.However, the calling method requires us to throw one more exception, Exception2.As it is throwing Exception2, we need to declare it by using the throws clause.
From the Developer's point of view, the method m2() handles exception Exception1 thrown by m1(), but leaves to the calling method of m2() to handle Exception2.
If you modify the method m2() as follows :

In the above code, the calling method, m2() decides not to handle Exception1 thrown by method m1().Although, syntatically this is correct, the Developer might get an impression that method m2() is throwning Exception1 and Exception2.But actually this is not the case.


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.[..]


As I mentioned you can catch all the exceptions that is thrown by the method in the try block.You can also catch Exception directly, but again from Developer point of view, it is not the correct thing to do.For example, to catch Exception instead of Exception1 is legal, but the Developer, who examines the code of m2() would feel that m1() is actually throwing Exception, not Exception1!
So as a general rule, catch only those exceptions that are thrown.
The same is the case with the throws clause.If your method is throwing a subclass of an Exception, you can declare it throw either the subclass or the Exception as a whole.But again as I said think from the Developer point of view.The moment you declare a method throws Exception (instead of its subclass), the Developer starts to think, he has to take care of all the Exceptions that can be possibly thrown.Would you want this to happen?
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited August 30, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic