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

