Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Overriding method signatures with exceptions

 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having some doubts with the rules about the signature of method declaration when they throw exceptions.
Can anybody correct me if I am wrong?
1 - When overriding methods, the throws Exception clause of the overriding class may throw:
a) No exception
b) The same exception
c) A subclass of the exception thrown in the parent method
d) A RuntimeException (Added by JM)
It can�t throw:
a) An exception that doesn�t follow the below rules (parent exception of the exception thrown in the parent method or an exception that doesn�t belong to the parent exception hierachy)
2 - When overriding constructors that throws exceptions:
a) The overriding class must throw the same exception or a parent of the exception thrown in the parent constructor.
b) If a) is done, the overring constructor can throw another exceptions that doesn�t need to belong to the parent exception hierachy.

[This message has been edited by Marcela Blei (edited July 24, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can add one more thing to your list:
Overriding method can also throw a runtime exception in its declaration.
thanks
JM the danger
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks JM I�ve just tried it, and added to my previous message
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marcela,
I have doubts regarding your second point i.e,
2 - When overriding constructors that throws exceptions:
a) The overriding class must throw the same exception or a parent of the exception thrown in the parent constructor.
b) If a) is done, the overring constructor can throw another exceptions that doesn�t need to belong to the parent exception hierachy.
Can you explain it. What do you mean by overriding constructors. I mean, is it a valid term. Is it constructors of the subclass? Can't they throw exceptions that are subclass of the exception thrown by constructor in parent class. Can you please explain your point.
Thanks
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RuntimeException and Error can be thrown by any method or
constructor regardless of the throws clause.
 
rajsim
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are some more rules for exceptions that apply during
object creation and finalization
1 - instance initializer blocks
they can not have any throws clause and they may throw
exceptions that can be thrown by all the constructors
i.e. exceptions common to all the constructors.
2 - static initializer blocks can not throw any exceptions.
3 - finalize method may throw exceptions but they are ignored
and object is garbage collected regardless
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KN:
I �ll try to explain your doubts
>Can you explain it. What do you mean by overriding >constructors. I mean, is it a valid term.
No it isn�t a valid term, you are right, constructors doesn�t inherit one from others. When you extend a class, your constructor calls the default constructor of the parent class (if you don�t call another parent constructor explicitly) and then executes the extending class�s constructor. That�s why you need to catch or declare the throws statement in the new constructor, is like calling a method that has a throws clause.
> Can't they throw exceptions that are subclass of the exception >thrown by constructor in parent class. Can you please explain >your point.
No, they can�t. I you think about my first explanation you will understand by not.
I expected to be clear, if not, ask me again.
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rajsim for your contribution!
I didn�t understand:
>1 - instance initializer blocks
>they can not have any throws clause and they may throw
>exceptions that can be thrown by all the constructors
>i.e. exceptions common to all the constructors.
Can you explain this with an example?
>2 - static initializer blocks can not throw any exceptions.
This is a good point testing vs the 1) you give.
>3 - finalize method may throw exceptions but they are ignored
>and object is garbage collected regardless.
A good point to remember too!
Thanks again.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcela,


>1 - instance initializer blocks
they can not have any throws clause and they may throw
exceptions that can be thrown by all the constructors
i.e. exceptions common to all the constructors.


Instance initialization blocks just begin and end with a curly braces. They are implicitly associated with the context of the class and have no "named" context like methods. Hence they don't have a declaration part, and so there is no place to have the throws clause!!.
However, since you can have almost any legal code within the instance initialization block, it can have exception raising code too, ie, the throw..... statements. Since it is a usual practice to put common code shared by all constructors in the instance initialization blocks, exceptions common to all constructors can be thrown by them.
Am I making it clear, or have I successfully confused you even more ?
Ajith
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>Am I making it clear, or have I >successfully confused you even more ?
You are allways clear Ajith, I tried the following code, that was what you mean?
public class Base {
int i;
{
i = 5;
System.out.println(5);
throw new Exception(); //This line
//doesn�t compile
//alone
}
//But if you declare the throws in the
//constructor it works!!!
Base() throws Exception {}
}

[This message has been edited by Marcela Blei (edited July 25, 2000).]
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic