系统异常与应用程序异常的清晰解释 [英] A clear explanation of system exception vs application exception

查看:14
本文介绍了系统异常与应用程序异常的清晰解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPA 规范区分系统异常和应用程序异常.我对线的确切位置感到有些困惑.我的猜测:

The JPA spec differentiates between system exceptions and application exceptions. I am a bit confused about where the line is drawn exactly. My guesstimate:

应用程序异常是您的代码或代码使用的库显式或隐式抛出的异常.

An application exception is an exception that your code or libraries used by your code throw, explicitly or implicitly.

  • 这是否包括所有异常、运行时和检查(无论来源)?

系统异常可能是持久化提供者抛出的异常.它当然包含 javax.persistence.PersistenceException 的所有子类.

A system exception is probably an exception thrown by the persistence provider. It certainly contains all subclasses of javax.persistence.PersistenceException.

  • 提供程序代码抛出的其他异常呢?
  • 其他 Java EE 库抛出的异常呢?
  • 如果异常被包裹在 EJBException 中,会有什么不同吗?
  • What about other exceptions thrown by provider code?
  • What about exceptions thrown by other Java EE libraries?
  • Does it make a difference if the exception is wrapped in an EJBException?

如何使用 ApplicationException 注释?我从未见过它被使用过.

How can I influence the behaviour by using the ApplicationException annotation? I have never seen it being used yet.

推荐答案

当出现业务逻辑错误而不是系统错误时,应抛出应用程序异常.

An application exception shall be thrown when there is a business-logic error, as opposed to a system error.

有一个重要区别:应用程序异常不会自动导致事务回滚.客户端有机会在抛出应用异常后恢复.

There is an important difference: application exceptions do not automatically cause a transaction to rollback. The client has an opportunity to recover after an application exception is thrown.

应用程序异常被发送到客户端而不被重新打包为 EJBException.因此,您可以使用它们来报告验证错误或业务逻辑问题,并将它们传递给客户端.

Application exceptions are sent to the client without being repackaged as an EJBException. Therefore, you can use them to report validation errors or business logic problems, and they will reach the client.

这是否包括所有异常、运行时和检查,而不管来源?

Does this include all exceptions, runtime and checked regardless the source?

没有.默认情况下,应用程序异常是不扩展 RuntimeException 或 RemoteException 的异常.您可以更改此设置,如下所述.

No. By default, application exceptions are the exceptions that do not extend RuntimeException or RemoteException. You can change this, as described below.

如何使用 ApplicationException 影响行为注释?

How can I influence the behaviour by using the ApplicationException annotation?

如果您希望事务自动回滚,您可以使用@ApplicationException(rollback=true).

You can use @ApplicationException(rollback=true) if you want the transaction to be rolled back automatically.

您也可以在 RuntimeException 和 RemoteException 的子类上使用注解,以避免包装为 EJBExceptions,并定义它们的自动回滚行为.

You can also use the annotation on subclasses of RuntimeException and RemoteException, in order to avoid wrapping as EJBExceptions, and define their automatic rollback behavior.

其他 Java EE 库抛出的异常呢?

What about exceptions thrown by other Java EE libraries?

它们将遵循相同的规则,但您可以使用 XML 描述符将第三方类声明为应用程序异常(带或不带自动回滚).

They will follow the same rules, but you can use the XML descriptor to declare third party classes as application exceptions (with or without automatic rollback).

提供程序代码抛出的其他异常呢?

What about other exceptions thrown by provider code?

不确定,我认为您很少会看到来自提供程序代码的非系统错误(远程或运行时异常).

Not sure, I think you'd rarely see a non system error (Remote or Runtime exceptions) coming from provider code.

如果异常被包裹在一个 EJBException 中会有什么不同吗?

Does it make a difference if the exception is wrapped in an EJBException?

是的.它会影响您在客户端代码中处理异常的方式.

Yes. It will impact how you handle exceptions in the client code.

(参考:Enterprise JavaBeans 3.0,Bill Burke,O'Reilly)

(Ref: Enterprise JavaBeans 3.0, Bill Burke, O'Reilly)

希望对大家有所帮助.

这篇关于系统异常与应用程序异常的清晰解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆