JSTL Core <c:catch> Tag

Advertisements


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.

Attribute:

The <c:catch> tag has following attributes:

AttributeDescription RequiredDefault
varThe name of the variable to hold the java.lang.Throwable if thrown by elements in the body.NoNone

Example:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:catch> Tag Example</title>
</head>
<body>

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

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

</body>
</html>

This would produce following result:

The exception is : java.lang.ArithmaticException: / by zero
There is an exception: / by zero


Advertisements
Advertisements