• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Constructors and Exception

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


You must define an explicit constructor for class BullDog to compile. Compiler can't insert implicit constructor here as it usually does.

Why can't compiler insert implicit constructor here (with throws clause),
because it has info about parent class.


Regards,
cmbhatt
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are throwing a checked exception.

Default constructor -
<access modifier of the class> CLASSNAME() <No exceptions inserted> {super();}

This checked exception is not thrown, so any class creating an instance is not aware of the exception. Hence the code doesn't compile
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Chandra asked:

Why can't compiler insert implicit constructor here (with throws clause),
because it has info about parent class.



Because constructors are not inherited.

And as class DogFamily contains nothing but a constructor, there's not much info in it.
If you extend DogFamily, the subclass must have a constructor with the same parameters (no parameters here) and it must throw the same exception. Or a superclass of this exception.

Bu.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Burkhard & Swarna!

Burkhard, I liked the very first line:
Because constructors are not inherited!





Regards,
cmbhatt
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more observation:


The above is how the compiler default constructor would look like and it gives the following compiler error:
Exception 'java.io.IOException' thrown by the field initializer or the constructor is not caught and does not appear in throws clause of the constructor

Now I thought what if I try to catch it like following...


The compiler is still not happy becuase the word super() has to be the first line of the constructor and seems like word "try" tried to overthrow "super()" from its supermost position

So the only way it would let atleast the two classes compile is



Now again I mistakenly thought that with the above code segment whenever I try to instantiate BullDog It will throw a exception at runtime too. But it doesnt... It just needs the declaration of the error or catching ofcourse just for compiler

See the following code...




Thanks,
Megha
[ April 27, 2007: Message edited by: megha joshi ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if you want throw checked exceptions from a constructor, then wrap them in a CoderMalfunctionError.
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess its a pun on the word Coder that you meant

from the API


public class CoderMalfunctionErrorextends ErrorError thrown when the decodeLoop method of a CharsetDecoder, or the encodeLoop method of a CharsetEncoder, throws an unexpected exception.



 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good observation Megha,

I will play with that!!!



Thanks,
cmbhatt
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic