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 Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
I had come accross that constructors cannot specify exceptions in the header but,the following code compiles without error.
class A
{
A()throws Exception{}
}



Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
Savio,
Constructor declarations can have a list of thrown constructors. I don't see why not...since constructors can throw exceptions. You may want to check out Bill Brogdens exam cram bk - pg 67. The JLS also mentions it.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
saumil shukla
Ranch Hand

Joined: Dec 01, 2000
Posts: 47
Since consturcots are functions, think of possibility of opening I/O's in a constructor. If it fails to creat I/O it should throw exception.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi All,
Contstructors can include a 'throws' clause but you need to be careful if you use super() in a subclass to call the default constructor in it's superclass.
The following code produces a compile error:

Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
Jane,
The explicit constructor invocation super() has nothing to do
with the compiler-error you get. I copied and attempted to compile your code and I got the following compiler error:
"The exception java.lang.Exception is not handled".
This is because, while the constructor of B throws a checked exception, your main() method neither handles it (in a try-catch block), nor throws it (yes, main methods too can throw exceptions; of course if and when the exception actually occurs,
the main method throwing it will result in thread termination).
Hope this clarifies things,
Panagiotis.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Panagiotis,
You're right; it's not the super() specifically that causes the error.
The point I was trying to make was that while constructors can throw exceptions you need to be careful in designing a subclass that uses a throws clause in one of it's constructors and relies on a default constructor in a superclass; because a default constructor does not include a throws clause.
Apologies if the point wasn't clear in the example.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
[This message has been edited by Jane Griscti (edited December 02, 2000).]
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
What? Remember: constructors are not inherited. That means if your subclass can throw anything you want.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Adrian,
Constructors are not inherited, you're right; but, when a your create an instance of a class Java will first instantiate instances of all it's superclasses. If your subclass constructor does not specifically reference a superclass constructor, the JVM will automatically call the superclass default constructor. Please refer to JLS§12.4.1, §12.5.

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor