jspexceptionjstlcustom-tags

Can I throw exception with JSTL in a custom JSP tag when an attribute is invalid?


I'm making some JSP tag. I pass a type parameter to the tag.

The code look like this:

<%@ attribute name="type" require="true" %>
<c:choose>
    <c:when test="${type eq 'even'}">
        <c:set var="remainder" value="0" />
    </c:when>
    <c:when test="${type eq 'odd'}">
        <c:set var="remainder" value="1" />
    </c:when>
    <c:otherwise>
        <%-- Want to throw exception!! --%>
    </c:otherwise>
</c:choose>

I want to throw an exception if I pass a wrong value. When I searched about this subject I just found this. Can't I throw a normal exception in JSTL?


Solution

  • Just throw to notice the problem immediately

    Who would "notice" ? That exception can only be caught by the container and it will result in some "500 Internal Server Error" page (or some custom error page you define).

    If that's what you really want, you can define a custom tag that will just perform "throw new WhateverException()", and then make use of your custom taglib like: <mytaglib:reportError/>, as the standard core taglib did not consider throwing an exception would ever be desirable for anyone.