JSTL Catch Tag <c:catch>

JSTL catch tag is used to catch the exception thrown at run time in JSP. The <c:catch> tag catches any Throwable that occurs in its body and optionally exposes it. Simply it is used for error handling and to deal more gracefully with the problem.

JSTL Catch Tag Example:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<title>c:remove Tag Example</title>
<c:set var="salary" scope="session" value="${100000}"/>
<p>Before Remove Value: <c:out value="${salary}"/></p>
<c:remove var="salary"/>
<p>After Remove Value: <c:out value="${salary}"/></p>

<c:catch var ="catchException">
   <% int x = 5/0;%>

<c:if test = "${catchException != null}">
   <p>The exception is : ${catchException} <br />
   There is an exception: ${catchException.message}</p>


As you can see above, we are trying to display result of dividing firstNumber with secondNumber. It may be possible that secondNumber is 0. In that event, exception will be thrown at run time. <c:catch> block will handle this exception and print the exception in the next <c:if> block.

<<Previous <<   || Index ||   >>Next >>