• 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

Exceptions in constructors

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

I have this code



I do know that, constructor of a sub class can throw any exception but it must also throw exception from super class or its superclass exception.

My question is, in class Child when it says Exception will be thrown from Child() constructor, why not compiler complains something like,
already declared exception or unreachable or redundant throws for FileNotFoundException and IOException?
as both of these are subclasses of Exception.


Thanks,
With kind regards,
Prathima
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors don't override each other, like with methods! So the rule about which exceptions are allowed for overriding a method does not apply to constructors.

The Parent constructor can throw an IOException. So Child (a subclass of Parent) constructor must declare at least IOException, because super() will be invoked (which possible throws IOException). That's the only requirement for the Child constructor. But Child constructor itself can throw any other exception as well.

You can declare this method without any compiler errors, so why would it be different with constructors?


Hope it helps!
Kind regards,
Roel
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either in method or in constructor, why is the compiler not complaining about the already thrown exception?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you might want to write something like this:

 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Matthew correct me if I am wrong,



"someOtherMethodThatMightThrowOtherTypesOfException" should be either Checked or Unchecked, If unchecked, then no problem as it is not checked by the compiler, if checked then it will be taken care by Exception which is already thrown, is it not right?

Thanks,
With kind regards,
Prathima
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prathima gaitonde wrote: "someOtherMethodThatMightThrowOtherTypesOfException" should be either Checked or Unchecked, If unchecked, then no problem as it is not checked by the compiler, if checked then it will be taken care by Exception which is already thrown, is it not right?


Not completely correct!

Assume the someOtherMethodThatMightThrowOtherTypesOfException method throws a checked exception. You know you have to handle or declare this exception, otherwise you'll get a compiler error. So you could either add the appropriate checked exception (or the Exception class) to the throws clause of the constructor (as shown in the code snippet). Or you could put the method call in a catch block, as shown in this code snippet:

As always unchecked exceptions are not checked by the compiler, so you can handle and/or declare these exceptions, but it's not required.

Note: when you declare an exception in the throws clause of a method/constructor, it just indicates that this method/constructor could (sometimes) throw this exception. That statement is not throwing the exception, to actually throw an exception, you need a throw statement. An example:


And you know the drill by know, it's time for a pop quiz What about this code snippet, will it compile or not? And if not, what's the reason why it doesn't compile?


Hope it helps!
Kind regards,
Roel
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:it's time for a pop quiz What about this code snippet, will it compile or not? And if not, what's the reason why it doesn't compile?



Hi Roel,

Feeling happy to reply instantly, without a second thought, NO the code wont compile, the reason is, if present super() should be the first line of code in the constructor.

With great respect,
Prathima
 
author & internet detective
Posts: 40722
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prathima gaitonde wrote:Feeling happy to reply instantly, without a second thought, NO the code wont compile, the reason is, if present super() should be the first line of code in the constructor.


Correct! Glad you got that right and confidently too!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prathima gaitonde wrote:Feeling happy to reply instantly, without a second thought, NO the code wont compile, the reason is, if present super() should be the first line of code in the constructor.


Spot-on! Well done
 
Grow a forest with seedballs and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic