I want to throw a custom exception
, somewhat like the following, from a servlet whenever some specific problem occurs.
public class CustomException extends Throwable {
private String[] errArray;
public CustomException(String[] errArray){
this.errArray = errArray;
}
public String[] getErrors(){
return errArray;
}
}
And then when this exception is thrown I want to redirect the user to a specific error page.
<error-page>
<exception-type>com.example.CustomException</exception-type>
<location>/WEB-INF/jsp/errorPage.jsp</location>
</error-page>
Here is the error page, I want to use the exception implicit object.
<%@ 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>
Now the problem occurs when I go to add CustomException
in throws
declaration of the servlet's doGet
method. I get the following error:
Exception CustomException is not compatible with throws clause in HttpServlet.doGet(HttpServletRequest, HttpServletResponse)
Now how can I overcome the problem? Is it even possible to custom exception like this & forward to error page when It is thrown? Or is there any other way? Thanks in advance :)
HttpServlet
class doGet
throws ServletException
as declared below:
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
Please make your CustomException
to extend ServletException
to comply with the method specification.
EDIT: In your error.jsp, get the errors as:
<% String[] errArray = null;
if(exception instanceof CustomException) {
errArray = (CustomException)exception.getErrors();
}
%>
Please note: It returns String[]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.