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 🙂
HttpServletclassdoGetthrowsServletExceptionas declared below:Please make your
CustomExceptionto extendServletExceptionto comply with the method specification.EDIT: In your error.jsp, get the errors as:
Please note: It returns
String[]