This is really weird Matt.

There is nothing that I know of that would be causing your code not to catch the exception. Are you getting the DataIntegrityException?

Also have you steped it through the debugger?

TTFN,

-bd-

On Nov 29, 2004, at 9:00 AM, Matt Raible wrote:

I'm experiencing a strange issue. I am calling a Spring-managed bean from my managed-bean and for some reason, the bean won't catch a checked exception. Here's the method that throws the exception:

public void saveUser(User user) throws UserExistsException {
try {
dao.saveUser(user);
} catch (DataIntegrityViolationException e) {
throw new UserExistsException("User '" + user.getUsername() +
"' already exists!");
}
}


In my bean I have:

       try {
           userManager.saveUser(user);
       } catch (UserExistsException ue) {
           log.warn(ue.getMessage());

// for some reason - MyFaces doesn't seem to catch this exception
} catch (DataIntegrityViolationException e) {
log.warn(e.getMessage());
addError("errors.existing.user",
new Object[] { user.getUsername(), user.getEmail() });


           return "editProfile";
       }

Is there some sort of rule that says "only catch Runtime Exceptions"? Also, is there anything in MyFaces/JSF for exception handling? Spring/Struts/WebWork all allow me to configure a specific page to go to when a particular exception is thrown. I'd rather not do it in web.xml since I tend to use that for more generic exceptions. Here's how I do it with Struts:

1.  struts-config.xml:

<global-exceptions>
<exception key="error.required" type="org.springframework.dao.DataAccessException"
path="/dataAccessFailure.jsp"/>
</global-exceptions>


2.  dataAccessFailure.jsp:

<%@ include file="/taglibs.jsp" %>

<h3>Data Access Failure</h3>
<p>
<c:out value="${requestScope['org.apache.struts.action.EXCEPTION'].message}"/ >
</p>


<!--
<%
Exception ex = (Exception) request.getAttribute("org.apache.struts.action.EXCEPTION");
ex.printStackTrace(new java.io.PrintWriter(out));
%>
-->


<a href="<c:url value='/'/>" onclick="history.back(); return false">&#171; Back</a>

Thanks,

Matt




Reply via email to