JSTL - Balise <c: catch> principale

le <c:catch> tag attrape tout Throwablequi se produit dans son corps et l'expose éventuellement. Il est utilisé pour la gestion des erreurs et pour traiter le problème de manière plus élégante.

Attribut

le <c:catch> tag a les attributs suivants -

Attribut La description Obligatoire Défaut
var Le nom de la variable contenant le java.lang.Throwable s'il est lancé par des éléments dans le corps. Non Aucun

Exemple

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

Le code ci-dessus générera le résultat suivant -

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