How to handle an exception in JSP?

The <c:catch> tag catches any Throwable that occurs in its body and optionally exposes it. It is used for error handling and to deal more gracefully with the problem.


The <c:catch> tag has the following attributes −

varThe name of the variable to hold the java.lang.Throwable if thrown by elements in the body.NoNone


<%@ taglib uri = "" prefix = "c" %>
      <title><c:catch> Tag Example</title>
      <c:catch var ="catchException">
         <% int x = 5/0;%>
      <c:if test = "${catchException != null}">
         <p>The exception is : ${catchException} <br />
         There is an exception: ${catchException.message}</p>

The above code will generate the following result −

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

I love programming (: That's all I know