GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Exception Hnadling in inheritence Concepts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Hnadling in inheritence Concepts" Watch "Exception Hnadling in inheritence Concepts" New topic
Author

Exception Hnadling in inheritence Concepts

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi

I know that when we are doing overriding , the child class method can deal with fewer exceptions than that are defined in Parent class Methods .

But why is it compulsary to deal with all exceptions when coming to constructors .

For example :



The above is not a valid code .

Its becomes valid when and only the Child constructor also declares SQLException.

Why is such a behaviour in constructors ??


Save India From Corruption - Anna Hazare.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
Write down the path of control on a piece of paper and it will become obvious.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Didn't get you campbell
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

The child constructor always calls one of the parent constructors*; if you leave out the call to "super(...)" one will be added implicitly without any arguments. This whole process of a child class constructor calling its super class constructor is called constructor chaining, and it will only end when Object's non-argument constructor is called.

In this case, the parent constructor can throw an exception so the child constructor must, as always, either catch it or declare throw it. Since the first statement must always be a call to "super(...)" or "this(...)" (as said before, if you don't add it the compiler will) catching is not allowed so the only option is to throw it.

* Or another constructor of the same class if the first call is to "this(...)".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks for the great explanation .

Can you please tell me actually what does this mean then :

Constructors are not inhereted!

Thank you .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Consider this example:
Even though class TS has a constructor that takes a String, and you can call new T("Hello World"), class Child does not so calling new Child("Hello World") will not work.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


Now i am understanding why do we need to override different constructors that are present inside the Exception class when extending the Exception class .


Thanks once again .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
Ravi Kiran V wrote:
Now i am understanding why do we need to override different constructors that are present inside the Exception class when extending the Exception class .


Thanks once again .
Look at the constructors. Why do you think they have to be overridden?
 
GeeCON Prague 2014
 
subject: Exception Hnadling in inheritence Concepts