Java 系统异常与应用程序异常的清晰解释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19563088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A clear explanation of system exception vs application exception
提问by kostja
The JPA spec differentiates between system exceptions and application exceptions. I am a bit confused about where the line is drawn exactly. My guesstimate:
JPA 规范区分系统异常和应用程序异常。我对线的确切位置感到有些困惑。我的猜测:
An application exception is an exception that your code or libraries used by your code throw, explicitly or implicitly.
应用程序异常是您的代码或代码使用的库显式或隐式抛出的异常。
- Does this include all exceptions, runtime and checked regardless the source?
- 这是否包括所有异常、运行时和检查,无论来源?
A system exception is probably an exception thrown by the persistence provider. It certainly contains all subclasses of javax.persistence.PersistenceException
.
系统异常可能是持久性提供者抛出的异常。它当然包含javax.persistence.PersistenceException
.
- 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
?
- 提供程序代码抛出的其他异常呢?
- 其他 Java EE 库抛出的异常呢?
- 如果异常被包裹在一个 中,会有什么不同
EJBException
吗?
How can I influence the behaviour by using the ApplicationExceptionannotation? I have never seen it being used yet.
如何使用ApplicationException注释影响行为?我从未见过它被使用过。
采纳答案by jjmontes
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.
有一个重要的区别:应用程序异常不会自动导致事务回滚。客户端有机会在抛出应用程序异常后恢复。
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.
应用程序异常被发送到客户端而不被重新打包为 EJBException。因此,您可以使用它们来报告验证错误或业务逻辑问题,并且它们将到达客户端。
Does this include all exceptions, runtime and checked regardless the source?
这是否包括所有异常、运行时和检查,无论来源?
No. By default, application exceptions are the exceptions that do not extend RuntimeException or RemoteException. You can change this, as described below.
否。默认情况下,应用程序异常是不扩展 RuntimeException 或 RemoteException 的异常。您可以更改此设置,如下所述。
How can I influence the behaviour by using the ApplicationException annotation?
如何使用 ApplicationException 注释影响行为?
You can use @ApplicationException(rollback=true) if you want the transaction to be rolled back automatically.
如果您希望事务自动回滚,您可以使用 @ApplicationException(rollback=true)。
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.
您还可以在 RuntimeException 和 RemoteException 的子类上使用注释,以避免包装为 EJBExceptions,并定义它们的自动回滚行为。
What about exceptions thrown by other Java EE libraries?
其他 Java EE 库抛出的异常呢?
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).
它们将遵循相同的规则,但您可以使用 XML 描述符将第三方类声明为应用程序异常(带或不带自动回滚)。
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.
不确定,我认为您很少会看到来自提供程序代码的非系统错误(远程或运行时异常)。
Does it make a difference if the exception is wrapped in an EJBException?
如果异常被包装在 EJBException 中,这有什么不同吗?
Yes. It will impact how you handle exceptions in the client code.
是的。它会影响您在客户端代码中处理异常的方式。
(Ref: Enterprise JavaBeans 3.0, Bill Burke, O'Reilly)
(参考:Enterprise JavaBeans 3.0、Bill Burke、O'Reilly)
I hope it helps a bit.
我希望它有点帮助。
回答by Yoi
I feel, I must add this very clear description, that Mahesh Desai gave on Coderanch :
我觉得,我必须添加这个非常明确的描述,这是 Mahesh Desai 在 Coderanch 上给出的:
Any exception that is a subclass of Exception, but not a subclass of RuntimeException and RemoteException, is an application exception. All the application exceptions are checked exceptions, So, when we call another method in our method that can throw an application exception, we must either declare it in the throws clause of the calling method or catch it in the body of the calling method or both.
All system exceptions are unchecked exceptions except RemoteExceptions and it can't be handled by the user.
任何属于 Exception 子类但不是 RuntimeException 和 RemoteException 子类的异常都是应用程序异常。所有应用程序异常都是检查异常,因此,当我们在我们的方法中调用另一个可以抛出应用程序异常的方法时,我们必须在调用方法的 throws 子句中声明它或在调用方法的主体中捕获它,或两者兼而有之.
除 RemoteExceptions 外,所有系统异常都是未经检查的异常,用户无法处理。