wood burning stoves*
The moose likes Servlets and the fly likes Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Exception handling" Watch "Exception handling" New topic
Author

Exception handling

Raymond Ou
Ranch Hand

Joined: Jun 03, 2003
Posts: 34
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

Joined: Jan 30, 2003
Posts: 1108
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.


"No one appreciates the very special genius of your conversation as the dog does."
Raymond Ou
Ranch Hand

Joined: Jun 03, 2003
Posts: 34
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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Jun 03, 2003
Posts: 34
I will take a look, thanks, Bill.
Your website is cool.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling