Java Tutorial/JSTL/Exception

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

Catch Error

index.jsp



   <source lang="java">

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.ru/jstl/fmt" %>

Peter"s Junk-Mail Service

<c:if test="${param.submitted}">

 <c:if test="${empty param.name}" var="noName" />
 <c:if test="${empty param.email}" var="noEmail" />
 <c:if test="${empty param.age}" var="noAge" />
 <c:catch var="ageError">
   <fmt:parseNumber var="parsedAge" value="${param.age}" />
   <c:if test="${parsedAge < 13}" var="youngAge" />
 </c:catch>
 <c:if test="${not empty ageError}" var="badAge" />
 <c:if
  test="${not (noName or noEmail or noAge or badAge or youngAge)}">
   <c:set value="${param.name}" var="name" scope="request"/>
   <c:set value="${param.email}" var="email" scope="request"/>
   <c:set value="${param.age}" var="age" scope="request"/>
   <jsp:forward page="spamFormHandler.jsp" />
 </c:if>

</c:if> <form method="post">

Thanks for signing up for our junk-mail service. Once you submit your information on the form below, you"ll begin to receive all the "spam" you ever wanted.

 <input type="hidden" name="submitted" value="true" />

Enter your name: <input type="text" name="name" value="<c:out value="${param.name}"/>" />
<c:if test="${noName}"> Note: you must enter a name </c:if>

Enter your email address: <input type="text" name="email" value="<c:out value="${param.email}"/>" />
<c:if test="${noEmail}"> Note: you must enter an email address </c:if>

Enter your age: <input type="text" name="age" size="3" value="<c:out value="${param.age}"/>" />
<c:choose> <c:when test="${noAge}"> Note: you must enter your age </c:when> <c:when test="${badAge}"> Note: I couldn"t decipher the age you typed in </c:when> <c:when test="${youngAge}"> Note: You"re too young to receive pornographic junk mail. Please grow older and try again. </c:when> </c:choose>

 <input type="submit" value="Sign up" />

</form></source>





Catch 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 Exception and Print Out Exception Message

   <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>





C: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>

So you want to make a call from your cell phone?!

Checking the signal strength...

   <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!!!
     </c:when>
     <c:otherwise>
       No Exception!!!
     </c:otherwise>
   </c:choose>
 </body>

</html></source>





Generate Exception in c:out

   <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>