<c:catch> tag is used to handle the exceptions that can be raised by any of the tag which are located inside the body of the tag. The tag <c:catch> is member of core tag library of jstl.
The <c:catch> tag catches any throwable that occurs in the body of the tag. This tag is used to handle exception during run time.
Syntax For c:catch Tag
[code lang=”html”]
<c:catch> attributes> body content </c:catch>
[/code]
The tag accepts the var attribute, which takes the name of the variable that stores the exception thrown.
Example of c:catch Tag
[code lang=”html”]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<span style="font-size: 12px; line-height: 18px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" </span>
<span style="font-size: 12px; line-height: 18px;">"http://www.w3.org/TR/html4/loose.dtd"></span>
<span style="font-size: 12px; line-height: 18px;"><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %></span>
<span style="font-size: 12px; line-height: 18px;"><html></span>
<span style="font-size: 12px; line-height: 18px;"><head></span>
<span style="font-size: 12px; line-height: 18px;"><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%></span>
<span style="font-size: 12px; line-height: 18px;"><html></span>
<span style="font-size: 12px; line-height: 18px;"><head></span>
<span style="font-size: 12px; line-height: 18px;"><title>JSTL c:catch Example</title></span>
<span style="font-size: 12px; line-height: 18px;"></head></span>
<span style="font-size: 12px; line-height: 18px;"><body></span>
<c:catch var="CatchNullPointerException">
<span style="font-size: 12px; line-height: 18px;"> <% String str = null; str.trim(); %></span>
</c:catch>
<c:if test="${CatchNullPointerException != null}">
<p>
Exception is : ${CatchNullPointerException} <br />
</p>
</c:if>
</body>
</html>
[/code]
How To Run?
- Save this file as example.jsp in your eclipse IDE.
- Now select this jsp file, right mouse click and select Run as ->Run on server
Output
When the execution process is completed successfully we will get the following output :
Advantages of c:catch Tag
- It is used to handle exception generated during run time.
- This tag is used to capture any throwable exception that happens on the body of a page.
Previous Tutorial : JSTL Core Out Tag :: Next Tutorial : JSTL Core c:if Tag
Leave a Reply