I came across this question: which of the following statements are true about constructor? 1. a class inherits its constructor from its parent class 2. a constructor may have a return type 3. a constructor may throw exception 4. compiler will assign a default constructor if a class does not explicitly declare one. I chose 4 only, but the answer is 3 and 4. but when I read Khalid books, it clearly say constructor can not throw exceptions. Do I have a real bad memory? Second question about constructor, I know it has been discussed before, but I want to clarify my confusion. Because some books say, constructor can be defined final, but Khalid book again claims that there is no other modifiers permitted except accessibility modifier preceding constructor. Any body step out to shed some light on this?
hi i dont know about hte modifiers but i am pretty sure i reda somewhere that a constructer can throw an exception and it makes sense since you can do everything in a constructor there will be exceptions you will have to throw
Constructors can definitely throw exceptions..... As for them being final, the RHE and Exam Cram books clearly mention that they cannot be final, static, volatile or transient. At the same time they can be private, protected or public.
I agree with Shafeeq Sheikh that Constructors can definitely throw exceptions. One precaution there... Per Bruce Eckel (TIJ, P-392) - If your class is a derived class then its constructor must declare any base-class constructor exceptions in its exception specification. Because a base-class constructor will always be called one way or another.