JSTL - Core <c: catch> Tag

Il <c:catch> tag cattura qualsiasi Throwableche si verifica nel suo corpo e facoltativamente lo espone. Viene utilizzato per la gestione degli errori e per affrontare il problema con maggiore garbo.

Attributo

Il <c:catch> tag ha i seguenti attributi:

Attributo Descrizione necessario Predefinito
var Il nome della variabile per contenere java.lang.Throwable se lanciata da elementi nel corpo. No Nessuna

Esempio

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

Il codice sopra genererĂ  il seguente risultato:

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