JSTL <c:catch> Tag Example

JSTL catch tag is used to catch the exception thrown at run time in JSP.


Syntax:

<c:catch var ="variable_name">
 //Set of statements in which exception can occur
</c:catch>

Here variable_name can be any variable in which exception message can be stored. If there is an exception occurs in the code enclosed enclosed with <c:catch> then this variable contains the exception message. Let’s understand this with the help of an example.

JSTL catch tag is used to catch the exception thrown at run time in JSP.


Syntax:

<c:catch var ="variable_name">
 //Set of statements in which exception can occur
</c:catch>

Here variable_name can be any variable in which exception message can be stored. If there is an exception occurs in the code enclosed enclosed with <c:catch> then this variable contains the exception message. Let’s understand this with the help of an example.


Example:

In this example we are throwing arithmetic exception by dividing an integer with zero and then we are printing the exception variable (which contains the exception message) using Expression language.

If there is no exception in the block of statements in <c:catch> then the variable (exception) should have null value. That’s why we are checking exception!=null before printing the variable’s value.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>c:catch JSTL core tag example</title>
</head>
<body>
	<c:catch var="exception">
		<%
			int x = 10 / 0;
		%>
	</c:catch>
	<c:if test="${exception != null}"> Occurred exception is : ${exception} </c:if>
</body>
</html>


Output in Browser :

JSTL c:catch tag





comments powered by Disqus