Hafsa Abdul gafoor
Hafsa Abdul gafoor

Reputation: 135

Catch division by zero in JSTL

I am trying to catch a division by zero by the following jstl loop using the catch block but I never the 'Not applicable' but it throws '?%' instead. I get the 'grade' displayed correctly. How can I display 'Not applicable' instead of '%?'?

  <td style="text-align: center;">
    <c:set var="grade" value="${(G / (G + L + W + D + A + pc + dc + vc) * 100)}"/>
      <c:catch>
      <fmt:formatNumber value="${grade}" pattern="0" var="myInteger"/>
      <c:set var="passed" value="${myInteger - grade eq 0}"/>
      </c:catch>

       <c:if test="${passed}">
       <fmt:formatNumber minFractionDigits="2" maxFractionDigits="2"
                                          value="${grade}" />% 
       </c:if>

       <c:if test="${not passed}">
                        Not applicable
                    </c:if>
                </td>
            </tr>
        </c:forEach>

Upvotes: 0

Views: 638

Answers (1)

Amit Bhati
Amit Bhati

Reputation: 5649

Your statement which is throwing the Arithematic Exception is not within the catch block of JSTL (i.e.Use logic like below):-

  <c:catch var="errorOccurred">
  <c:set var="grade" value="${(G / (G + L + W + D + A + pc + dc + vc) * 100)}"/>
  </c:catch>

 <c:choose>
 <c:when test="${errorOccurred != null}"> 
    Not applicable
</c:when> 
<c:otherwise> 
 <fmt:formatNumber value="${grade}" pattern="0" var="myInteger"/>
 <c:set var="passed" value="${myInteger - grade eq 0}"/>
    <c:choose>
        <c:when test="${passed}"> 
            <fmt:formatNumber minFractionDigits="2" maxFractionDigits="2"
                                      value="${grade}" />% 
        </c:when> 
        <c:otherwise> 
         Not applicable  
        </c:otherwise>
    </c:choose>
    </c:otherwise> 
    </c:choose>

Upvotes: 1

Related Questions