• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Hnadling in inheritence Concepts

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write down the path of control on a piece of paper and it will become obvious.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't get you campbell
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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(...)".
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic