File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors and Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors and Exception" Watch "Constructors and Exception" New topic
Author

Constructors and Exception

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


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


cmbhatt
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Burkhard & Swarna!

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





Regards,
cmbhatt
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
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

Joined: Aug 03, 2002
Posts: 7729
So if you want throw checked exceptions from a constructor, then wrap them in a CoderMalfunctionError.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
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

Joined: Feb 28, 2007
Posts: 1707
Good observation Megha,

I will play with that!!!



Thanks,
cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors and Exception