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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 236
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: 24183

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: 38075
And please use the code tags.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19656

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: 236
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: 3432
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: 236
thank you guys for your advice and reply
jQuery in Action, 2nd edition
subject: why constructor needs throws clause
Similar Threads
Reg. Exception hierarchy
Unreachable Code Question
Exceptions and Overriding
Collection Example in K&B
Why no compilation error?