JSTL

If you have written some code which you think that it can invoke a error, and still you are confident that you can recover from that solution by yourself. Then then is one tag provided to you that is which is one of the tag of core action librar

JSTL

JSTL <c: catch>

        

If you have written some code which you think that it can invoke a error, and still you are confident that you can recover from that solution by yourself. Then then is one tag provided to you that is <c:catch> which is one of the tag of core action library. It works like a try/catch block in java. 

If we have any doubt that the particular code can invoke a error then we should write those codes inside the <c:catch> core action tag. The beauty of this tag is that it works both like a try and catch. There is no such thing like try tag. This tag can handle both the try and catch situation. 

In this example we are going to use the var attribute of the <c:catch> core action. This attribute is optional that means it is not mandatory. This attribute is used when we want to access the exception after the end of the <c:catch> tag.  In this example we have also used one more tag that is <c:if>, which we are using to check the condition. 

The code of the program is given below:

 

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>Catching the Exception</title>
</head>
<body>
<strong>I can catch the exception:</strong><br>
<c:catch var ="catchException">
The exception will be thrown inside the catch:<br>
<% int x = 5/0;%>
</c:catch>

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

</body>
</html>

The output of the program is given below:

Download this example.