Java/JSTL/Exceptions
Версия от 18:01, 31 мая 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>