3 Replies Latest reply: Dec 10, 2012 11:51 PM by Kayaman RSS

    Not able to handle the exception in web application

    955481
      Hi,
      In my web application i m trying to handle the run time exception with the help of <exception-type> and <location> tag in web.xml file as show below.

      But whille running the application the exeception raised when n>5 is not handled and i am getting internal server error with root cause as " coreservlets.DumbDeveloperException: Value of n is greater than 5 ".

      I have kept all the files in the proper path and directory and i am using Tomcat 7.0.27.

      Kindly help me how to sort it out.

      web.xml
      <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
          <error-page>
              <exception-type>coreservlets.DumbDeveloperException</exception-type>
              <location>/WEB-INF/jspPages/DDE.jsp</location>
          </error-page>
      </web-app>
      the other files of my web applications are given below.

      DumbDeveloperException.java
      package coreservlets;
      
      public class DumbDeveloperException extends Exception {
        public DumbDeveloperException() {
          super("Value of n is greater than 5");
        }
      
        public static int dangerousComputation(int n)
            throws DumbDeveloperException {
          if (n < 5) {
            return(n + 10);
          } else {
            throw(new DumbDeveloperException());
          }
        }
      }
      RiskyPage.jsp
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
      <HTML>
          <HEAD><TITLE>Risky JSP Page</TITLE></HEAD>
          <BODY BGCOLOR="#FDF5E6">
             <h2>Risky Calculations </h2>
              <%@ page import="coreservlets.*" %>
              <% int n = ((int) (10 * Math.random()));%>
              <UL>
                  <LI>n: <%= n%>
                  <LI>dangerousComputation(n): <%= DumbDeveloperException.dangerousComputation(n)%>
              </UL>
          </BODY>
      </HTML>
      DDE.jsp
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
      <HTML>
          <HEAD><TITLE>Dumb</TITLE></HEAD>
          <BODY BGCOLOR="#FDF5E6">
              <H2>Dumb Developer</H2>
              We're brain dead. Consider using our competitors.
          </BODY>
      </HTML>