Click to See Complete Forum and Search --> : Change exception message in error page


annie519
07-11-2003, 07:07 AM
How can I display my own exception message without the default messages that come with it? I have several exception classes defined, and when I throw a NumberFormat Exception, it prints out my custom message, but it also gives me the default message:

Server caught unhandled exception from servlet [FreezeSrc]: This is from the ErrChainServletExceptionClass. You've entered an invalid number : afgrht. Try again.

Any help you can provide would be great!! I'm new to this, so I don't know if I've put too much code in my jsp's/servlets.

Sorry if this is too much info, but here's what I've created.

I've created an exception class:
public class ErrChainServletException extends ServletException {
Exception e = null;

public ErrChainServletException() {
super();
}

...
public ErrChainServletException(String arg0, Exception e) {
super("This is from the ErrChainServletExceptionClass. " + arg0, e);
this.e = e;
}
}

And I've got a request handler servlet which handles the requests:
public class RequestHandler extends HttpServlet {

HashMap handlerMap = new HashMap();

...

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
performTask(req, resp);
} catch (Exception ex) {
try {
sendErrorRedirect(req,resp,"error.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
performTask(req, resp);
} catch (Exception ex) {
try {
sendErrorRedirect(req,resp,"error.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}

protected void sendErrorRedirect(HttpServletRequest req, HttpServletResponse resp,
String errorPageURL, Throwable e)
throws ServletException, IOException {

req.setAttribute ("javax.servlet.jsp.jspException", e);
getServletConfig().getServletContext().
getRequestDispatcher(errorPageURL).forward(req, resp);
}

This is my error page:
<html>
<head>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
isErrorPage="true" import="java.io.*"
%>
<TITLE>Error Occurred - <%= ErrorReport.getErrorCode() %></TITLE>
</head>
<body>
<jsp:include page="header.html" flush="true"></jsp:include>
<p><hr><p>
<font face="Georgia" color="black">An error has occurred: </font>
<p>
<font face="Georgia" color="red"><bold><blockquote>
Exception Message = <p>
<%= exception.getMessage() %>
</blockquote></bold></font>
<p>
<hr>
</body>
</html>

And this is my servlet that throws the exception:

public class FreezeSrc extends HttpServlet {

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
performTask(req,resp);
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
performTask(req, resp);
}

public void performTask(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException, NumberFormatException {
try {
String comment = req.getParameter("comment");
String sourceStr = req.getParameter("source");

int source = 0;
if (sourceStr != null && !sourceStr.equalsIgnoreCase("")) {
source = Integer.valueOf(sourceStr, 16).intValue();
}

FreezeGroup fg = new FreezeGroup(LoginInfo.getId(), LoginInfo.getPassword());
DWGroupFreezeInfo gfi = new DWGroupFreezeInfo();
gfi.setSource(source);
gfi.setComment(comment);
fg.submit(gfi);

RequestDispatcher rd = getServletContext().getNamedDispatcher("ShowFrozenDBs");
rd.forward(req, resp);
}
catch (NumberFormatException nfe) {
throw new ErrChainServletException("\nYou've entered an invalid number : "+nfe.getMessage() + ". \nTry again.\n", nfe);
}
catch (DWLoginException lc) {
throw new DWLoginServletException(lc.getMessage());
}
catch (DWServerComException sce) {
throw new DWServerComServletException(sce.getMessage());
}
catch (ErrorChainException ec) {
throw new ErrChainServletException(ec.getClass().getName() + " - Message: "+ ec.getMessage(), ec);
}
}
}

Khalid Ali
07-11-2003, 08:51 AM
Wrong place to post your Java related question,
Please repost your question at this link

http://forum.java.sun.com/