Java/JSTL/Exceptions
Содержание
Catch an Exception?
<source lang="java">
<%@ 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}" />
</c:catch> <c:if test="${e!=null}">The caught exception is: <c:out value="${e}" />
</c:if> <c:if test="${e==null}">No exception was thrown
</c:if> </body>
</html>
</source>
Catch an Exception in JSTL
<source lang="java">
<%@ 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}" />
</c:catch>
<c:if test="${e!=null}">The caught exception is: <c:out value="${e}" />
</c:if> <c:if test="${e==null}">No exception was thrown
</c:if> </body>
</html>
</source>
JSTL: catch Exception
<source lang="java">
<%@ 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>
</source>
JSTL: Catch with if
<source lang="java">
<%@ 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>
</source>
JSTL Exception: UnCaught Exception
<source lang="java">
<%@ 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}" />
</body>
</html>
</source>
JSTL Throw an Exception
<source lang="java">
<%@ 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}" />
</body>
</html>
</source>