aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " why constructor needs throws clause" Watch " why constructor needs throws clause" New topic
Author

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[])
{
try
{
xml l= new xml();
l.parse(RATE_TABLE_SOURCE);

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


}
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
regards
amir
[ October 23, 2008: Message edited by: Amirtharaj Chinnaraj ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Sheriff

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

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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

http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/helpers/XMLReaderAdapter.html

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

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

Create a new adapter.

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

Throws:
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 ]

Joanne
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
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