Java2all.com  

By Mechanism





Share This Topic -

JSP Exception handling with try catch:

InputData.html :

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>InputData.html</title>
  </head>
  <body>
    <form action="../JSPFILE/Calculator.jsp">
    <input type="text" name="n1"> <br>
    <input type="text" name="n2"> <br>
    <input type="submit" value="ADD">
    </form>
  </body>
</html> 

Calculator.jsp :

 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
  <head>
   <title> Calculator.jsp </title>
  </head>
  
  <body>
  <% try
  	 {
  	 	int i1 = Integer.parseInt(request.getParameter("n1"));
  	 	int i2 = Integer.parseInt(request.getParameter("n2"));
  	 	int add = i1 + i2;
  	 	out.print("Addition = "+add);
  	 }
  	 catch(NumberFormatException ne)
  	 {
  	 	out.print("Esception : "+ne);	
  	 }	
  %>
   
  </body>
</html> 

URL :

http://localhost:8080/JAVA_PROJECT/HTMLFILE/InputData.html

 

Java2All.Com

 

Input the integer value in text fields and click ADD button.

The browser display the below message,

Addition = 11

Now, input the float value in any of the text field and click ADD button so the browser display the message,

 

Esception : java.lang.NumberFormatException: For input string: "6.3"

Java2All.Com