✅ JSTL (especially Core and SQL libraries) has built-in error handling mechanisms
that allow you to catch and manage exceptions gracefully inside the JSP, without crashing the page.
You don’t always need to forward to a special error page — sometimes you can catch errors inline.
🔵 Key Concepts in JSTL Error Handling
| Feature | Description |
|---|---|
errorPage and isErrorPage | Same as normal JSP pages (for big/serious errors) |
<c:catch> tag | Catch exceptions that happen during JSTL actions inside a JSP page |
| JSTL SQL error handling | Special attributes like varStatus to capture database errors |
| Silent failure (optional) | JSTL often fails gracefully without throwing exceptions into the page |
🔥 1. Using <c:catch> to Handle Errors Locally
<c:catch> is a JSTL tag that catches exceptions happening inside its body and stores them into a variable.
✅ Syntax:
<c:catch var="error">
<%-- Code that might throw exception --%>
</c:catch>
<c:if test="${not empty error}">
<p>Error occurred: ${error.message}</p>
</c:if>
✅ If an error happens, it will be caught and stored into error variable instead of breaking the page.
📜 Practical Example:
Imagine dividing by zero:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:catch var="error">
<%
int x = 5 / 0; // This will throw ArithmeticException
%>
</c:catch>
<c:if test="${not empty error}">
<p style="color:red;">An error occurred: ${error.message}</p>
</c:if>
✅ Output:
An error occurred: / by zero
Page does not crash!
User sees a friendly message.
🔵 2. Error Handling in JSTL SQL Tags
When using JSTL SQL tags (like <sql:query>, <sql:update>),
errors can happen (like bad connection, bad query).
✅ You can capture errors using the var and varStatus attributes.
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<sql:query var="result" dataSource="jdbc/mydb">
SELECT * FROM unknown_table
</sql:query>
<c:if test="${not empty result.error}">
<p style="color:red;">SQL Error: ${result.error}</p>
</c:if>
✅ If the query fails, you can detect it using result.error.
⚠️ Important: JSTL SQL tags are for prototyping/demo only — not recommended in real apps (use JDBC, JPA, or Spring Data for production).
🔵 3. JSTL Default Behavior (Silent Failures)
✅ Some JSTL tags (like <c:forEach>) fail silently if they encounter minor problems:
- Empty collections → no exception.
- Missing variables → just an empty output.
Example:
<c:forEach var="item" items="${nonexistentList}">
<p>${item}</p>
</c:forEach>
- Even if
nonexistentListisnull, no crash — JSTL just skips the loop.
✅ Fail-safe by design — to keep user pages alive.
🎯 Summary Table
| Feature | Handling |
|---|---|
| Big page-wide exceptions | Use errorPage + isErrorPage |
| Local small exceptions | Use <c:catch> to capture and handle |
| SQL-related errors | Use varStatus.error or handle inside <c:catch> |
| Silent failures | JSTL skips minor errors gracefully |
📢 Important Practical Tip:
In professional projects, combine:
- JSTL local error handling (
<c:catch>) for small recoverable errors - Centralized error pages (via
web.xml) for big application failures
✅ That gives you the best user experience and system reliability.