Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Customer exception handling

 
Raymond Ou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the container would try and catch a RuntimeException would it? Have you tried extending Exception?
 
Raymond Ou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
};
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic