• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Customerized exception handling

 
Raymond Ou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem on customerized exception handling, 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, it go to find the InsufficientFundsException class instead of Exception_Error.html.
I copy the above code from the book of SCWCD Exam Study kit.
Thank you,
Raymond
 
Raymond Ou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, is there anybody can answer my question?
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I had to look this up but it's good preparation for test. From what I can tell you need the full exception class name not just Insufficient FundsException under Exception-Type.
I'd try that and see if it works.
 
Raymond Ou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ken, webserver is Tomcat 4, I am wondering whether there are something I need to setup for the container releasing handling my Exception.
Below is the code :
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;
}
};
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your current deployment descriptor say? In your first post you had the following:
<error-page>
<exception-type>InsufficientFundsException
</exception-type>
<location>/ErrorPages/Exception_Error.html
</location>
</error-page>
But you need the fully qualified class name for exception-type. So it should be: MyException.InsufficientFundsException.

Also you've called it InsufficientFundsException in web.xml but you're using InsufficientFundsException_Declarative in your actual code. So exception-type should probably really be MyException.InsufficientFundsException_Declarative.
Hope this helps (and works!)
Ken
 
Raymond Ou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken, you are right, this afternoon I changed the exception_type in web.xml to InsufficientFundsException_Declarative for other testing, but it fail again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic