wood burning stoves 2.0
The moose likes Beginning Java and the fly likes  why constructor needs throws clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " why constructor needs throws clause" Watch " why constructor needs throws clause" New topic

why constructor needs throws clause

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 241
hi guys
iam trying to write a pice code that connectos to url and gets a response in xml its done purely using core java

but when i write the following pice of code my IDE is showing error

public class xml extends XMLReaderAdapter {

private HashMap rates = new HashMap();

public static final String RATE_TABLE_SOURCE = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
public xml() // line 1 throws Exception

rates.put("EUR", new Double(1.00));

public static void main(String as[])
xml l= new xml();

System.out.print("rates ");
catch(Exception ex)

but when i remove the comment in the line 1 my coding compiles if not my IDE is showing me error in that line

my question is constructors are not members of class then why its asking to decalre throws class in the construstor like overriding methods require
looking for your methods
[ October 23, 2008: Message edited by: Amirtharaj Chinnaraj ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24193

Apparently the superclass constructor (i.e., the no-argument constructor of class XMLReaderAdapter) declares that it throws some exception type. When a subclass object is constructed, the superclass constructor is always also called. Since there's no way for your subclass constructor to catch or cancel any exceptions thrown by the superclass constructor, the only remaining choice is to declare that the subclass constructor throws the same exception.

You can change your declaration to throw only the specific type thrown by XMLReaderAdapter, whatever that is.
[ October 23, 2008: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44500
And please use the code tags.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20129

XMLReaderAdapter's parameter-less constructor can throw a SAXException. Therefore, so can your constructor.

Normally you have the choice of throwing the exception further or catching it, but since the (implicit) call to super() must be the first statement in a method you can't catch it.

How To Ask Questions How To Answer Questions
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 241
thanks rob and Ernest
for your reply i have checked the API
for the class org.xml.sax.helpers.XMLReaderAdapter in the below link
i didnt find any throws clause specified in that


please reply me why throws clause is not mentioned in the API section
looking for your replies
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Which bit did you read ?
public XMLReaderAdapter()
throws SAXException

Create a new adapter.

Use the "org.xml.sax.driver" property to locate the SAX2 driver to embed.

SAXException - If the embedded driver cannot be instantiated or if the org.xml.sax.driver property is not specified.

[ October 23, 2008: Message edited by: Joanne Neal ]
[ October 23, 2008: Message edited by: Joanne Neal ]

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 241
thank you guys for your advice and reply
I agree. Here's the link: http://aspose.com/file-tools
subject: why constructor needs throws clause
jQuery in Action, 3rd edition