JSTL c:catch Example

JSTL c:catch Example explains about How to use c:catch tag inside a JSTL Page for catching an exception.

Consider an example, where you need to catch an exception and you need to show formatted error message to user in a friendly manner, In that use case you can use JSTL c:catch

c:catch Tag is available as a part of JSTL core tags, c:catch Tag captures any throwable exceptions that happens on the body of a page

You can see the below example demonstrating the usage JSTL c:catch Example inside JSTL, Here we are capturing java.lang.NullPointerException and catching using c:catch Tag.

Required Libraries

You need to download

  1. Tomcat 7
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

JSTL c:catch Example

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL c:catch Example</title>
</head>
<body>
	<c:catch var="CatchNullPointerException">
		<% String str = null; str.trim(); %>
	</c:catch>
	<c:if test="${CatchNullPointerException != null}">
		<p>
			Exception is : ${CatchNullPointerException} <br />
		</p>
	</c:if>
</body>
</html>
Output
Exception is : java.lang.NullPointerException 
  

 






Your email address will not be published. Required fields are marked *