现有如下两个程序:XMLObject.java
==========================================================================================
package com.firepond.build;import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;public abstract class XMLObject {
    protected abstract Document getDocument()throws ParserConfigurationException;    protected Document loadNewDocument(String tag) throws ParserConfigurationException {
     DocumentBuilder builder = domFactory.newDocumentBuilder();
     Document doc = builder.newDocument();
        Element root = doc.createElement(tag);      // Create Root Element
        doc.appendChild(root);                          // Add Root to Document
        root.appendChild(doc.createTextNode("\n"));
Element global = createNewDocumentData(doc);
if (global != null) {
    indent(root, 1);
    root.appendChild(global);
    root.appendChild(doc.createTextNode("\n"));
}
        return doc;
    }
    
    ...}
==========================================================================================Build.java
==========================================================================================
package com.firepond.build;import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;import org.xml.sax.InputSource;
import org.xml.sax.SAXException;public class Build extends XMLObject {
private Element m_dom = null;    protected void ensureDocumentLoaded() throws ParserConfigurationException  {
if (m_dom == null) loadNewDocument("buildset");
    }    protected Document getDocument() throws ParserConfigurationException{
ensureDocumentLoaded();
return m_dom.getOwnerDocument();
    }
...
}
==========================================================================================Build.java是继承自XMLObject.java的类,在实现XMLObject的抽象方法getDocument()时,我需要向外抛出ParserConfigurationException,
此时我总是编译不过去,报错为:Exception ParserConfigurationException is not compatible with throws clause in XMLObject.getDocument()请问这个问题我该如何改正?

解决方案 »

  1.   

    如果interface不允许抛出,你就不能抛出了!
    你使用其它方法解决吧,比如RuntimeException
      

  2.   

    Got it! 
    Thank you, java2000_net!R U still OT now?
    And I also got this answer from javaranch.... Put it here to help other ones...
    http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=33&t=025570&p=1
    ==============================================
    An abstract method has to declare all the checked exceptions that an implementation might reasonably throw.Implementations are allowed to throw those checked exceptions, plus any unchecked exceptions (RuntimeException and subclasses). But they do not have to do so. It is perfectly legal, and indeed advisable, for any implementations that do not need to throw a particular checked exception to omit the declaration that they do so.Care should be taken in the design of your abstract method. You should not make it dependent on particular implementations. Here's an example...Say you have an abstract method storeData(), whose purpose is to write some data to a persistent storage mechanism, which might be a file or database, but could also be something you haven't dreamt of at the time of writing.The naive approach would be to declare that it throws IOException (for files) and SQLException (for databases). But then what if someone wants to write a network-based implementation, using methods that throw all sorts of network exceptions?The better approach is to define your own exception type, DataStorageException, and declare that storeData() throws that. Then each implementation should convert its own exception to a DataStorageException.Converting an exception in this way is often called "wrapping" the original exception. Note that you should store the original exception inside the DataStorageException, either at construction or by using initCause(). Doing so will hugely help debugging, later.