A clear explanation of system exception vs application exception

前端 未结 2 821
感动是毒
感动是毒 2020-12-12 20:52

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 appli

相关标签:
2条回答
  • 2020-12-12 21:35

    I feel, I must add this very clear description, that Mahesh Desai gave on 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.

    0 讨论(0)
  • 2020-12-12 21:38

    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.

    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.

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

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

    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.

    What about exceptions thrown by other Java EE libraries?

    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.

    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.

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

    I hope it helps a bit.

    0 讨论(0)
提交回复
热议问题