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
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, 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.
Joined: Aug 30, 2000
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).]
What? Remember: constructors are not inherited. That means if your subclass can throw anything you want.
Joined: Aug 30, 2000
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