Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors and Exception

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • 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
 
swarna dasa
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Burkhard & Swarna!

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





Regards,
cmbhatt
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • 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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Good observation Megha,

I will play with that!!!



Thanks,
cmbhatt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic