wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors:True or false Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors:True or false" Watch "Constructors:True or false" New topic
Author

Constructors:True or false

Karen Leoh
Ranch Hand

Joined: Dec 03, 2001
Posts: 40
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
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
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
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
just to add, that it need not declare exceptions if they are unchecked exceptions.


Amir
 
GeeCON Prague 2014
 
subject: Constructors:True or false