开发者

How can I make my Java Servlet class handle SAXException's?

When compiling my Java Servlet code, I get the following error...

in javax.servlet.http.HttpServlet; overridden method does not throw org.xml.sax.SAXException

In m开发者_如何学运维y overridden doGet() function, I'm using JAXP to process XML which apparently requires me to handle SAXExceptions. But when I tac "SAXExeption" onto the list of exception types I want my doGet function to handle, I get the above error. How can I get my doGet function to hangle SAXExcpetions?

thanks in advance for all your help!


You can't declare an overriding method that throws checked exceptions that aren't thrown by the method being overridden. In other words, since HttpServlet.doGet() is declared as throwing IOException and ServletException, you cannot use any other exception types in the throws clause of your doGet method.

However, you can wrap up the SAXException that you are getting as a ServletException to get around this:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
    try { 
        JAXP.possiblyThrowASAXException();
    } catch (SAXException e) {
        throw new ServletException("JAXP had a parsing failure", e);
    }
}


When a parent class declares to throw a checked exception, subclasses have to throw at least the same checked exceptions to fulfill the contract of the parent class. The other way round, a child class method does not have to be declared to throw any exception, but it can't be declared to throw a checked exception that the parent class method is not declared to throw.

To illustrate this, lets imagine you have the following class:

package test;

import java.io.IOException;

public class Parent {
    void foo() throws IOException {
        throw new IOException();
    }
}

This would compile:

package test;

class Child1 extends Parent {
    void foo() {
    }
}

But this would not:

package test;

import org.xml.sax.SAXException;

class Child2 extends Parent
{
    void foo() throws SAXException {
        throw new SAXException();
    }
}

The javac compiler would generate the following output:

test/Child2.java:6: foo() in test.Child2 cannot override foo() in test.Parent; overridden method does not throw org.xml.sax.SAXException
    void foo() throws SAXException {
         ^
1 error

In other words, you can't write this:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException, SAXException {
    super.doGet(req, resp);
    ...
}

You have to handle the SAXException in the doGet() method and to wrap it in a ServletException if you want to rethrow it.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    super.doGet(req, resp);

    try {
        // code that possibly throws a SAXException 
        ...
    } catch (SAXException e) {
        // handle it or rethrow it as ServletException
        ...
    }
}


I agree with the answer by @David Winslow

When you throw an Exception in the doGet method of an HttpServlet you are essentially throwing up your hands and giving up any sensible error handling.

Its probably a better approach to trap the SAXException and emit an error which gives useful information.

Is the client sending you information in the get which is XML? If it is, you should probably respond with a sensible error telling them that their XML is incorrect.

Is this error caused by something loaded on the Server side? If it is then there's a basic problem with the get and the user should get an appropriate message for that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜