Hi.. I just want to confirm whether the following statement is true or false Constructors can throw exceptions. If parent default constructor throw exception, subclass have to do the same or have to handle the exeption. Thanks.
--------------------<p>Karen Leoh<br />Sun Certified Programmer for Java™ 2 Platform
Constructors can throw exceptions. If parent default constructor throw exception, subclass have to do the same or have to handle the execption
the above statement is true. the subclass whose parent constructor throws exception ,has to handle the same exception or superclass of the exception thrown by the constructor of the super class. try the following code
This is because a subclass constructor will call the superclass constructor as its first job before executing anything else.
The superclass constructor may throw an exception which in turn may be thrown in the subclass constructor since the latter called the former. As far as exceptions are concerned, you can see the invocation to super() as a normal method invocation which may not terminate by returning gracefully but by throwing an exception instead. The thrown exception is to be handled by the subclass constructor (try-catch) or thrown ahead...