为什么抛出 EJBException 而不是 NoResulException?
Why EJBException is thrown instead of NoResulException?
我不知道为什么我无法捕获 EJB 中的方法抛出的 NoResultException...
try {
User user = userFacade.findByEmail(email);
userController.setSelected(user);
getSelected().setUserOid(user.getOid());
} catch (NoResultException noResultException) {
JsfUtil.addErrorMessage("No user found with provided email");
System.err.println(noResultException.getMessage());
} catch (EJBException e){
System.err.println(e.getMessage());
}
EJB
@Stateless
public class UserFacade extends AbstractFacade<User> {
...
public User findByEmail (String email) throws NoResultException{
User user = em.createNamedQuery("User.findByEmail", User.class).
setParameter("email", email).getSingleResult();
return user;
}
...
为什么此代码捕获 EJBException 而不是 NoResultException,我如何捕获 NoResultException?
Avvertenza: javax.ejb.EJBException
...
...
Caused by: javax.persistence.NoResultException: getSingleResult() did not retrieve any entities.
只有应用程序异常直接报告给客户端,即不包含在EJBException
中(注意:在container-managed事务划分的情况下,客户看到的是 EJBTransactionRolledbackException
。
然而 NoResultException
是一个 系统异常 ,这意味着 EJB 容器会将该异常包装在 EJBException
.
中
将@ApplicationException
注解应用于异常,使其成为应用程序异常。在这种情况下,您可能希望将 NoResultException
包装在您自己的带有上述注释的 NoSuchEmailException
(或类似的)中。
EJB 规范内容如下...
系统异常
A system exception is an exception that is a java.rmi.RemoteException (or one of its sub-classes) or a RuntimeException that is not an application exception.
申请异常
An application exception is an exception defined by the Bean Provider as part of the business logic of an application. Application exceptions are distinguished from system exceptions in this specification.
现在是重要的部分:
An application exception thrown by an enterprise bean instance should be reported to the client precisely (i.e., the client gets the same exception)
规范继续阐明应用程序异常的作用和使用:
Enterprise bean business methods use application exceptions to inform the client of abnormal application-level conditions, such as unacceptable values of the input arguments to a business method. A client can typically recover from an application exception.
An application exception may be a subclass (direct or indirect) of java.lang.Exception (i.e., a “checked exception”), or an application exception class may be defined as a subclass of the java.lang.RuntimeException.
(an “unchecked exception”).
我不知道为什么我无法捕获 EJB 中的方法抛出的 NoResultException...
try {
User user = userFacade.findByEmail(email);
userController.setSelected(user);
getSelected().setUserOid(user.getOid());
} catch (NoResultException noResultException) {
JsfUtil.addErrorMessage("No user found with provided email");
System.err.println(noResultException.getMessage());
} catch (EJBException e){
System.err.println(e.getMessage());
}
EJB
@Stateless
public class UserFacade extends AbstractFacade<User> {
...
public User findByEmail (String email) throws NoResultException{
User user = em.createNamedQuery("User.findByEmail", User.class).
setParameter("email", email).getSingleResult();
return user;
}
...
为什么此代码捕获 EJBException 而不是 NoResultException,我如何捕获 NoResultException?
Avvertenza: javax.ejb.EJBException
...
...
Caused by: javax.persistence.NoResultException: getSingleResult() did not retrieve any entities.
只有应用程序异常直接报告给客户端,即不包含在EJBException
中(注意:在container-managed事务划分的情况下,客户看到的是 EJBTransactionRolledbackException
。
然而 NoResultException
是一个 系统异常 ,这意味着 EJB 容器会将该异常包装在 EJBException
.
将@ApplicationException
注解应用于异常,使其成为应用程序异常。在这种情况下,您可能希望将 NoResultException
包装在您自己的带有上述注释的 NoSuchEmailException
(或类似的)中。
EJB 规范内容如下...
系统异常
A system exception is an exception that is a java.rmi.RemoteException (or one of its sub-classes) or a RuntimeException that is not an application exception.
申请异常
An application exception is an exception defined by the Bean Provider as part of the business logic of an application. Application exceptions are distinguished from system exceptions in this specification.
现在是重要的部分:
An application exception thrown by an enterprise bean instance should be reported to the client precisely (i.e., the client gets the same exception)
规范继续阐明应用程序异常的作用和使用:
Enterprise bean business methods use application exceptions to inform the client of abnormal application-level conditions, such as unacceptable values of the input arguments to a business method. A client can typically recover from an application exception.
An application exception may be a subclass (direct or indirect) of java.lang.Exception (i.e., a “checked exception”), or an application exception class may be defined as a subclass of the java.lang.RuntimeException. (an “unchecked exception”).