English 中文(简体)
我怎么能让我的 Java·塞莱特班处理SAXException?
原标题:How can I make my Java Servlet class handle SAXException s?
  • 时间:2009-12-01 05:10:50
  •  标签:
  • java
  • xml

在汇编我的 Java·塞莱特法典时,我有以下错误......

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

在我压倒一切的DoGet()职能中,Im利用日本宇宙航空研究开发机构处理XML,这显然要求我处理SAXExceptions。 但是,在我要求我的独角兽部队行使职能时,我会发现上述错误。 我怎样才能发挥我的 do作用,以hang死SAXExcpetions?

事先感谢你们的一切帮助!

问题回答

你们可以宣布一种压倒一切的方法,放弃被压倒一切的方法所抛弃的例外情况。 换言之,由于HttpServlet.doGet()被宣布为 throw弃了IOException和SerletException,你不能在你的DoGet方法的 throw条款中使用任何其他例外类型。

然而,你可以总结一下,你正在作为“服务对象”来接手:

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

当父母阶层宣布放弃一项受检查的例外时,子类必须至少放弃同样的受检查的例外,以履行母阶级的合同。 另一种方式是,儿童阶级方法不必宣布放弃任何例外,但不能宣布放弃父母阶级方法的经核实的例外。

为说明这一点,请想一下:

package test;

import java.io.IOException;

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

这将汇编:

package test;

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

但这不是:

package test;

import org.xml.sax.SAXException;

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

<代码>javac汇编者将产生以下产出:

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

换言之,你可以写道:

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

您必须处理<代码>SAXException in the doGet()方法,并在ServletException中填入。

@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
        ...
    }
}

我同意David Winslow的答复。

当你在M.Get方法中投下了一种HttpServlet的例外情况时,你基本上放弃了你的手,放弃了任何合理的错误处理。

这种做法可能是一种更好的办法,使SAXException陷入困境,并造成错误,从而提供了有用的信息。

客户在XML的收件中寄送你的信息吗? 如果是的话,你也许会以一个可察觉的错误来回答,告诉他们他们的XML是不正确的。

这一错误是否由装在服务器上的东西所造成? 如果是这样的话,便会遇到一个基本问题,使用者应当得到适当的信息。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签