系统异常与应用程序异常的明确说明 [英] A clear explanation of system exception vs application exception

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

问题描述

JPA规范区分了系统异常和应用程序异常。我有点困惑,在哪里绘制线。我的guestimate:

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?

不确定,我想你很少会看到来自提供商代码的非系统错误(远程或运行时异常)。 p>

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天全站免登陆