If the DataIntegrityViolationException was loaded twice by different Classloaders for some reason, the Class instances would not be equal to the JVM. So the saveUser might not catch it but the bean method does.
Try to make sure that your UserExistsException is really thrown.
HTH, Manfred
Matt Raible wrote:
Bill Dudney wrote:
This is really weird Matt.
You're telling me, I was baffled when I saw this behavior. I tried getting my UserManager as a managed property and from the servlet context - the issue happens using both strategies.
There is nothing that I know of that would be causing your code not to catch the exception. Are you getting the DataIntegrityException?
Yeah, it catches the RuntimeException (DataIntegrityViolation), but not the checked exception.
Also have you steped it through the debugger?
Yep. It sounds strange enough that I'm doing something wrong, but I'm pretty sure the issue below is what I experienced.
Matt
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">« Back</a>
Thanks,
Matt

