现有如下两个程序: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()请问这个问题我该如何改正?
解决方案 »
- java链接数据库问题
- java swing 中的问题,高手帮个忙啊,急!!!
- 帮忙写个正则式
- 如何使用鼠标的滚轮实现图形的放大与缩小
- 我的新手版Java Mp3播放器(源码),见笑了!
- 关于JTable单元格中输入中文,系统就报NullPointerException异常。输入英文就没事?
- 游戏是这样产生的--读thinking in java写的游戏
- 求一个过滤空字符串和重复字符串的java排序算法,在线等,解决马上结贴!
- 实现对学生的成绩按总分排序,程序代码如下:本人感谢java不久,望各位前辈多多指点,谢谢
- 又一简单问题, 字符替换……
- 一个简单的java题,但是难道很多人
- 求帮我解释以下的java概念!
你使用其它方法解决吧,比如RuntimeException
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.