File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to throw out a exception while implementing an abstract method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to throw out a exception while implementing an abstract method?" Watch "how to throw out a exception while implementing an abstract method?" New topic
Author

how to throw out a exception while implementing an abstract method?

Tom John
Greenhorn

Joined: Apr 15, 2002
Posts: 29
how to throw out a exception while implementing an abstract method?


I have following these 2 programs.

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();

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" is derived from its father "XMLObject". While implementing the abstract method "getDocument()", I throw out a ParserConfigurationException. Then I get this error:
==========================================================================================
Exception ParserConfigurationException is not compatible with throws clause in XMLObject.getDocument()
==========================================================================================

If I changed XMLObject as below
==========================================================================================
package com.firepond.build;
........
public abstract class XMLObject {
protected abstract Document getDocument()throws ParserConfigurationException;
........
==========================================================================================


I fixed this issue.

But XMLOject is implemented by many other class. Some of them don't need to throw out ParserConfigurationException.

So I don't know how to fix it...

Your help will be highly appreciated by me...
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
If you implement an abstract method, then the implementing method can only throw exceptions (or child classes of exceptions) that are specified in the abstract declaration. However, they don't have to throw these exceptions.
So, if you declare your abstract method to throw a ParserConfigurationException, any implementing methods can throw this exception, but they don't have to.


Joanne
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Tom John
Greenhorn

Joined: Apr 15, 2002
Posts: 29
Sing highly praise for your great help!!
You really did a great job for me!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to throw out a exception while implementing an abstract method?