JSTL - Core <c:catch> 標記



<c:catch> 標記會檢測其主體中出現的任何**Throwable**,並且可以選擇顯示它。它用於錯誤處理,並且可以更優雅地處理問題。

屬性

<c:catch> 標記具有以下屬性 −

屬性 描述 必需 預設值
var 如果主體中的元素丟擲,則用於儲存 java.lang.Throwable 的變數名稱。

示例

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

<html>
   <head>
      <title><c:catch> Tag Example</title>
   </head>

   <body>
      <c:catch var ="catchException">
         <% int x = 5/0;%>
      </c:catch>

      <c:if test = "${catchException != null}">
         <p>The exception is : ${catchException} <br />
         There is an exception: ${catchException.message}</p>
      </c:if>
   </body>
</html>

上述程式碼將生成以下結果 −

The exception is : java.lang.ArithmaticException: / by zero
There is an exception: / by zero
jsp_standard_tag_library.htm
廣告
© . All rights reserved.