Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling

 
Raymond Ou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can somebody help me in soluting the following problem?
I builded my own exception that was extended from RuntimeException, and setup the error-page element in web.xml to redirect the handling of exception to a servlet that handle all exceptions. But the servlet container ignored the setting in web.xml, and tried to find the exception class. How can I solute this problem?
Thanks,
Raymond
 
John Dunn
slicker
Ranch Hand
Posts: 1108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is your error? If it says it can't find the Exception you created, then it may be failing before it gets to redirect to your error page. When I tested this feature I used SQLException and IOException and forced those to be thrown. Try doing this and then replacing the 'throw new SQLException("my test")' call with your new exception's call. It may shed some light on what is happening.
 
Raymond Ou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John,
I wrote the my own exception class and placed it in the classpath, the container can handle the exception by this class.
What I am trying to do is to write a error handler to handle all the exception, like this:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import MyException.*;
public class BusinessLogicExceptionHandlerServlet extends HttpServlet
{

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter pw=response.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
Throwable exception=(Throwable) request.getAttribute("javax.servlet.error.exception");
if (exception instanceof InsufficientFundsException_Declarative){
pw.println("Sorry, you do not have enough fund.<br>");
}
pw.println("</body>");
pw.println("</html>");
}
};
InsufficientFundsException_Declarative is my own exception, it is a runtime exception.
And the following is set in web.xml:
<error-page>
<exception-type>InsufficientFundsException_Declarative</exception-type>
<location>/servlet/BusinessLogicExceptionHandlerServlet</location>
</error-page>
The problem is when I throw InsufficientFundsException_Declarative exception, the container try to locate this exception class, instead of BusinessLogicExceptionHandlerServlet.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the my own exception class and placed it in the classpath, the container can handle the exception by this class.

Some servers such as Tomcat 4 ignore your environment classpath and do their own thing so you will have to place the class where the server can see it. If you are using Tomcat, read the Class Loader How-To.
Bill
 
Raymond Ou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will take a look, thanks, Bill.
Your website is cool.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic