Exception Handling using JSTL tag….

Here in this post we will learn how to handle Exception in jsp page using JSTL tags

Here is the sample code for this app…


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<strong>Exception Handling Example</strong><br>
<a href="<c:url value='Check.jsp' />" >Go to Exception Page</a><br>
<%-- The above line is similar to response.encodeURL("url") method of servlet --%>

when we click on Go to Exception Page it will send u too Check.jsp page contain the following code:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<strong>Exception Handling Example</strong>
<c:catch var=”myexception” ><%—This is similar to try { }catch{} of normal exception handling –-%>
<strong>Inside the Catch Block : >></strong>
<% int x = 10/0; %>
<%– the <c:chosoe> , c:when and c:otherwise is equivalant to if {}else {} statement !! thanx –%>

<c:when test=”${myexception != null}” >
There was an Exception : ${myexception.message}<br><br>
<c:otherwise >
No Problem thanks!!
</c:otherwise >

at int x=10/0; there is devide by zero exception which is catch by the above given block

the running example can be found here


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s