This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Customer exception handling" Watch "Customer exception handling" New topic

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,
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.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
Connection m_Connection=(Connection) getServletContext().getAttribute("DB_Connection");
Statement m_Statement = m_Connection.createStatement();
String szSQL="select * from item_order where item_no='" + szItemID + "'";
ResultSet rs=m_Statement.executeQuery(szSQL);;
double dCurrentBalance=(double) rs.getInt("qty");
if (dCurrentBalance < dAmount){
throw new InsufficientFundsException_Declarative(dCurrentBalance, dAmount);
return dCurrentBalance;
}catch (SQLException e){
System.out.println("Database Access Error!");
return 0;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
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("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>");
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.");
I agree. Here's the link:
subject: Customer exception handling
It's not a secret anymore!