• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why constructor needs throws clause

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please use the code tags.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you guys for your advice and reply
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic