Catching Exceptions and Error Handling



ExceptionDemo.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head><title>Exceptions Demo</title></head>
<body>

<h3> Exception Demo </h3>

<form action = "ExceptionDemo.jsp" name="loginform" method="post">
<table>
<tr><td>  X </td><td> <input type="text" name="xfield" value="" length=10></td></tr>
<tr><td>  Y </td><td> <input type="text" name="yfield" value="" length=10></td></tr>
<tr><td colspan=2><input type="submit" value=" + " name="function">
        <input type="submit" value=" - " name="function">
        <input type="submit" value=" * " name="function">
        <input type="submit" value=" / " name="function">
</td></tr>
</table>
</form>
<p>
<%
  String xinput = request.getParameter("xfield");
  String yinput = request.getParameter("yfield");
  String function = request.getParameter("function");
  if(xinput != null && yinput != null && function != null)
  {
   try{
     double x = Double.parseDouble(xinput);
     double y = Double.parseDouble(yinput);
     double z=0;
     if(function.equals(" + "))
        z = x+y;
     if(function.equals(" - "))
        z = x-y;
     if(function.equals(" * "))
        z = x*y;
     if(function.equals(" / "))
        z = x/y;
    
%>
<p>
<b> <%= x +function+y+" = "+z %> </b>
<%
   }// end of try
   catch(NumberFormatException nfe)
   {
  
%>
<p>
<b><blink> X and Y must be integer numbers, try again!</blink></b>
<p>
<%= "Input Error : "+nfe.getMessage()+"\n"%>
<p>
<%
   java.io.PrintWriter writer = new java.io.PrintWriter(out);
   writer.println("Stack trace :");
   writer.println("<p>");
   nfe.printStackTrace(writer);
    }// end of catch
   } // end of if
%>

</body>
</html>




Error Pages

<%@ page errorPage = "ErrorPage.jsp" %>
    <%@page isErrorPage = "true" %>



ExceptionDemo2.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head><title>Exceptions Demo 2</title></head>
<body>
<%@ page errorPage = "ErrorPage.jsp" %>
<h3> Exception Demo </h3>

<form action = "ExceptionDemo2.jsp" name="loginform" method="post">
<table>
<tr><td>  X </td><td> <input type="text" name="xfield" value="" length=10></td></tr>
<tr><td>  Y </td><td> <input type="text" name="yfield" value="" length=10></td></tr>
<tr><td colspan=2><input type="submit" value=" + " name="function">
        <input type="submit" value=" - " name="function">
        <input type="submit" value=" * " name="function">
        <input type="submit" value=" / " name="function">
</td></tr>
</table>
</form>
<p>
<%
  String xinput = request.getParameter("xfield");
  String yinput = request.getParameter("yfield");
  String function = request.getParameter("function");
  if(xinput != null && yinput != null && function != null)
  {
     double x = Double.parseDouble(xinput);
     double y = Double.parseDouble(yinput);
     double z =0;
     if(function.equals(" + "))
        z = x+y;
     if(function.equals(" - "))
        z = x-y;
     if(function.equals(" * "))
        z = x*y;
     if(function.equals(" / "))
        z = x/y;
    
%>
<p>
<b> <%= x +function+y+" = "+z %> </b>
<%
  } // end of if
%>

</body>
</html>




ErrorPage.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page isErrorPage = "true" %>

<html>
<head><title>Error</title></head>
<body>
<h1>An error occurred while processing your request</h1>
<input type="button" value="click here to try again" onclick="javascript:history.back()">
<p>
<%= "Input Error : "+exception.getMessage()+"\n"%>
<p>
<%
   java.io.PrintWriter writer = new java.io.PrintWriter(out);
   writer.println("Stack trace :");
   writer.println("<p>");
   exception.printStackTrace(writer);
 %>
</body>
</html>