Exception Handling in jsp

Introduction of exception -: When a program is written against any programming language rules or syntax, then there is a possibility of exception on the program.If there is an exception in the entire programming page, then because of this, even without exception the code is not executed.

Like any other programming language In the JSP has same issue When a program is written against jsp rules or syntax, then there is a possibility of exception on the program.If there is an exception in the entire programming page, then because of this, even without exception the code is not executed. For example

<% 
    out.print("welcome in jsp exception"); 
      out.print(8/0);
 %>

On the example given above, the first statement is fine, but due to some exception in the second statement, even the first statement cannot be executed. This example will be read on try-Catch block.

What is the difference between exception and error

Exception -: Exceptions can be handled at the run-time. The exception object is thrown at run-time. If some incorrect information is provided by the user or not connected to the network or database then exception occurs. But these exceptions can be handled.

Error-: Errors cannot be handled. Errors are very critical. Program if out of memory goes. If there is a syntax error or if something goes wrong in the system or on the web server, then the error comes. It is very difficult to fix these errors.

Types of exception handling in JSP

In the jsp has two type exception

  1. Page level exception handling
  2. Try catch block exception handling

Page Level Exception Handling-: In page-level exception, a single error page is created. If some exception occurs in a JSP page, then the error page created without changing the url is executed. For page level exception handling program

In index.html page

<form action="print.jsp">
Enter Full_Name : <input type="text" name="Fname"><br />
Enter Age : <input type="text" name="fage"><br />
<input type="submit" value="Submit">

Print.jsp page-:First of all, if the exception is to be handled at the page-level, then the code to be handled is given the error Page of the page directive with the attribute as the code with exception handling as the value.

<%@ page errorPage="exception.jsp" %> 
<% 
out.print(request.getParameter("fname"));
out.print(Integer.parseInt(request.getParameter("fage")));
%>

exception.jsp-:When a code with exception handle is written, it is necessary to give the attribute and its value true to the is Error Page of the page directive in that code.

<%@ page isErrorPage="true" %>
<% out.print("Exception : " + exception); %>  

Try-Catch Block Exception Handling in JSP-: The exception is handled on a single page in the Try-Catch Block. For example

try{
	try_block_code;
}catch{
	exception_handling_code;
}

See the example of here..-:In the first statement on the example, welcome netnic is printed. No exception is likely to occur on this statement. But the code on which exception is likely to occur, then that code is given inside the try block and it is handled on the catch block.

<%
out.println("welcome netnic");

try{
out.println(4/0);
}
catch (Exception e){
	out.println("Exception : " + e);
}
%>

output of this programe

welcome netnic
Exception : java.lang.ArithmeticException: / by zero

Try-Catch-finally Block Exception Handling in JSP-:Finally, the code given on the block always executes whether an exception occurs or not. the syntax of

try{
	try_block_code;
}catch{
	exception_handling_code;
}finally{
	always_executed_code;
}

example of this

<%
out.println("welcome netnic");

try{
out.println(4/0);
}
catch (Exception e){
	out.println("Exception : " + e);
}finally{
	out.println("Always executed.")
}
%>

output of this

welcome netnic
Exception : java.lang.ArithmeticException: / by zero
Always executed.
More about java server page

 session implicit Object in jsp 

 pagecontext implicit object in jsp 

  application implicit Object in jsp

  config implicit Object in jsp

  out implicit Object in jsp  

 response implicit Object in jsp  

 implicit objects in jsp 

  Directives in jsp 

  Java server Pages Tags

  life cycle of jsp   

  What is java server pages 

  More about programming language 

Leave a Comment