【发布时间】:2012-10-28 06:40:48
【问题描述】:
每当出现某些特定问题时,我想从 servlet 中抛出一个自定义的 exception,有点像下面这样。
public class CustomException extends Throwable {
private String[] errArray;
public CustomException(String[] errArray){
this.errArray = errArray;
}
public String[] getErrors(){
return errArray;
}
}
然后当这个异常被抛出时,我想将用户重定向到一个特定的错误页面。
<error-page>
<exception-type>com.example.CustomException</exception-type>
<location>/WEB-INF/jsp/errorPage.jsp</location>
</error-page>
这里是错误页面,我想使用异常隐式对象。
<%@ page isErrorPage="true" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<my:head title="Error"></my:head>
<body>
<% String errArray = exception.getErrors(); %>
</body>
</html>
现在,当我在 servlet 的 doGet 方法的 throws 声明中添加 CustomException 时,就会出现问题。我收到以下错误:
Exception CustomException is not compatible with throws clause in HttpServlet.doGet(HttpServletRequest, HttpServletResponse)
现在我该如何克服这个问题?甚至可以自定义这样的异常并在抛出它时转发到错误页面?或者还有其他方法吗? 在此先感谢:)
【问题讨论】:
标签: java jakarta-ee exception servlets servlet-3.0