Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình

Cách xử lý exception trong JSP

Tag <c:catch> bắt bất kỳ Throwable nào xảy ra trong body và tùy chọn expose nó. Dùng cho error handling và xử lý vấn đề linh hoạt hơn.

Thuộc tính

AttributeDescriptionRequiredDefault
varTên biến chứa java.lang.Throwable nếu ném ra từ bodyNoNone

Ví dụ

<%@ taglib uri = "https://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>

Kết quả:

The exception is : java.lang.ArithmeticException: / by zero
There is an exception: / by zero