当父母阶层宣布放弃一项受检查的例外时,子类必须至少放弃同样的受检查的例外,以履行母阶级的合同。 另一种方式是,儿童阶级方法不必宣布放弃任何例外,但不能宣布放弃父母阶级方法的经核实的例外。
为说明这一点,请想一下:
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
...
}
}