Today I discovered something very useful. I considered myself quite expert about JSTL and I wonder how come I never noticed the existence of this interesting tag.
The tag I am referring to is the c:catch in the JSTL core library. This permits to simulate the classical try-catch blocks speaking in java terms. When you use in your JSP’s only JSTL tags it is maybe not so likely to incur in exceptions (at least if you do not divide by 0 or something like that …); but if you are using third party tags libraries you might encounter situations where you need to foresee the happening of an exception and prevent this to break the execution of your JSP. Once you know the exception can happen you can simply wrap it in a c:catch block and use the variable generated to perform the
Here below a snippet of the easiest situation you might face. Wrap your ‘dangerous code’ with a c:catch tag (which is the corresponding of the try section in java) and define the variable which will contain the potential Exception. If the variable will result to be null you will know that no exception occurred and you can go ahead with the execution of the standard code. Otherwise you might want to execute the alternative code (the equivalent of the code contained in the catch section in java)
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:catch var="signalException">
Dangerous code
</c:catch>
<c:choose>
<c:when test="${signalException == null}">
Exception occurs - Execute code block A
</c:when>
<c:otherwise>
No Exception - Execute code block B
</c:otherwise>
</c:choose>
0 Responses to “JSTL:catch”