This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Customer exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Customer exception handling" Watch "Customer exception handling" New topic
Author

Customer exception handling

Raymond Ou
Ranch Hand

Joined: Jun 03, 2003
Posts: 34
I have a problem on handling customerized exception , I add following line in web.xml,
<error-page>
<exception-type>InsufficientFundsException
</exception-type>
<location>/ErrorPages/Exception_Error.html
</location>
</error-page>
and define InsufficientFundsException as RuntimeException, the servlet do not catch the exception that is threw. However everytime I run the servlet, the container go to find the InsufficientFundsException class instead of Exception_Error.html, seems the web.xml did not effect.
Today I also try to set the 404 customer page, the static html work, but if I set the customer page to servlet, it fail.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I don't think the container would try and catch a RuntimeException would it? Have you tried extending Exception?
Raymond Ou
Ranch Hand

Joined: Jun 03, 2003
Posts: 34
However the container did try to catch my customized exception that was extended from RuntimeException, I don't know whether there is something I need to setup in the container. The webserver I used is Tomcat 4.
Raymond Ou
Ranch Hand

Joined: Jun 03, 2003
Posts: 34
Hi, I copy the code below:
1. (servlet that will throw a InsufficientFundsException_Declarative exception)
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import MyException.*;
public class DebitServlet_Declarative extends HttpServlet
{
private double Debit(String szItemID, double dAmount) throws InsufficientFundsException_Declarative
{
try{
Connection m_Connection=(Connection) getServletContext().getAttribute("DB_Connection");
Statement m_Statement = m_Connection.createStatement();
String szSQL="select * from item_order where item_no='" + szItemID + "'";
System.out.println(szSQL);
ResultSet rs=m_Statement.executeQuery(szSQL);
rs.next();
double dCurrentBalance=(double) rs.getInt("qty");
if (dCurrentBalance < dAmount){
throw new InsufficientFundsException_Declarative(dCurrentBalance, dAmount);
}else{
return dCurrentBalance;
}
}catch (SQLException e){
System.out.println(e.getMessage());
System.out.println("Database Access Error!");
return 0;
}
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String szItemID=(String)request.getParameter("itemid");
if (szItemID!=null){
double dAmount=Double.parseDouble(request.getParameter("amount"));
PrintWriter pw=response.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("Item ID=" + szItemID + "<br>");
double dCurrentBalance=Debit(szItemID,dAmount);
double dNewBalance=dCurrentBalance-dAmount;
pw.println("Your old balance was " + dCurrentBalance + " dollars.<br>You withdrew "+dAmount+" dollars.<br>");
pw.println("the new balance is " + dNewBalance +" dollars.<br>");
pw.println("</body>");
pw.println("</html>");
}
}
};
2. (InsufficientFundsException_Declarative exception)
package MyException;
import java.sql.*;
import javax.servlet.*;
public class InsufficientFundsException_Declarative extends RuntimeException
{
public double amtIntheAcc, amtTobeWithdrawn;
public InsufficientFundsException_Declarative (double pAmtIntheAcc, double pAmtTobeWithdrawn)
{
super("Your account has only " + pAmtIntheAcc + " dollars , you can not withdraw " + pAmtTobeWithdrawn + " dollars.");
amtIntheAcc=pAmtIntheAcc;
amtTobeWithdrawn=pAmtTobeWithdrawn;
}
};
 
jQuery in Action, 2nd edition
 
subject: Customer exception handling
 
Similar Threads
Study kit example doubt
error-page precedence
error handling in web.xml
Customerized exception handling
error pages in web.xml