Java/JSTL/Exceptions — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 06:17, 1 июня 2010

Catch an Exception?

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Catch an Exception?</title>
  </head>
  <body>
    <c:catch var="e">
    <c:set var="x" value="10" scope="page" />
    <c:set var="y" value="five" scope="page" />
    10 divided by 0 is 
    <c:out value="${10/0}" />
    <br />
    </c:catch>
    <c:if test="${e!=null}">The caught exception is:
    <c:out value="${e}" />
    <br />
    </c:if>
    <c:if test="${e==null}">No exception was thrown
    <br />
    </c:if>
  </body>
</html>





Catch an Exception in JSTL

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Catch an Exception</title>
  </head>
  <body>
    <c:catch var="e">
    <c:set var="x" value="10" scope="page" />
    <c:set var="y" value="five" scope="page" />
    x divided by y is 
    <c:out value="${x/y}" />
    <br />
    </c:catch>
    <br />
    <c:if test="${e!=null}">The caught exception is:
    <c:out value="${e}" />
    <br />
    </c:if>
    <c:if test="${e==null}">No exception was thrown
    <br />
    </c:if>
  </body>
</html>





JSTL: catch Exception

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>The c:catch action</title>
  </head>
  <body>
    <c:catch var="signalException">
      <%
        int i= (int) (Math.random() * 10);
        if (i < 5 )
          throw new NullPointerException(); %>
    </c:catch>

    <c:choose>
      <c:when test="${signalException != null}">
        Exception occurs.
      </c:when>
      <c:otherwise>
        No Exception.
      </c:otherwise>
    </c:choose>
  </body>
</html>





JSTL: Catch with if

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<%
  int i= (int) (Math.random() * 10);
  pageContext.setAttribute("signalStrength", new Integer(i), PageContext.PAGE_SCOPE);
%>
<html>
  <head>
    <title>The c:catch action</title>
  </head>
  <body>
    <c:if test="${pageScope.signalStrength < 5}">
      <c:set var="signalFailure" value="true" scope="page" />
    </c:if>
    <c:choose>
      <c:when test="${pageScope.signalFailure == true}">
        Exception occurs.
      </c:when>
      <c:otherwise>
        No Exception.
      </c:otherwise>
    </c:choose>
  </body>
</html>





JSTL Exception: UnCaught Exception

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Throw an Exception</title>
  </head>
  <body>
  <c:set var="x" value="10" scope="page" />
  <c:set var="y" value="five" scope="page" />
  x divided by y is 
  <c:out value="${x/y}" />
  <br />
  </body>
</html>





JSTL Throw an Exception

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Throw an Exception</title>
  </head>
  <body>10 divided by 0 is 
  <c:out value="${10/0}" />
  <br />
  </body>
</html>