<c:catch> JSTL tag is used in handling unusual situations that cause errors. It is implemented to catch any throwable exception that occurs in the program body during run time.



<c:catch> Tag Syntax

Syntax:

<c:catch var ="variable_name">
//Group of statements that may constitute exceptions during the execution of the program.
</c:catch>

<c:catch> Tag Attributes

Attribute Required Description
var No The name of the variable in which the exception message will be stored.

<c:catch> Tag Example

In this example, we are making an arithmetic exception by intentionally dividing the integer by zero. This is a common exception also known as "divided by zero exception" and is used to understand the runtime exception in almost all programming languages.

Example:

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<!DOCTYPE html>
<html>
<head>
    <title>Exception handling Example</title>
</head>

<body>
    <c:catch var ="exceptionCatch">
    <% int x = 4/0; %>
</c:catch>

<c:if test = "${exceptionCatch != null}">
    <p>Exception : ${exceptionCatch}</p>
    <p>Exception message : ${exceptionCatch.message}</p>
</c:if>

</body>
</html>

Output:

Exception : java.lang.ArithmeticException: / by zero
Exception message : / by zero

Any number divided by zero answers equal to infinity, and no data structure can store an infinite amount of data. Therefore, if a number is divided by zero, it will give an arithmetic exception.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram