Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A clear explanation of system exception vs application exception

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.

  • 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.

  • 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?

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

like image 922
kostja Avatar asked Oct 24 '13 10:10

kostja


People also ask

What is the difference between application exception and system exception?

As mentioned in above points system level exceptions are thrown by the . NET Common Language Runtime and used in almost all . Net applications. On other hand application level exceptions are thrown when a recoverable error has occurred, such as an invalid input argument values to a business method.

What is an application exception?

ApplicationException is thrown by a user program, not by the common language runtime. If you are designing an application that needs to create its own exceptions, derive from the ApplicationException class. ApplicationException extends Exception, but does not add new functionality.

What is a system exception?

SystemException is thrown by the common language runtime when errors occur that are nonfatal and recoverable by user programs. These errors result from failed runtime check (such as an array out-of-bound error), and can occur during the execution of any method.

What are the 3 types of exceptions?

There are three types of exception—the checked exception, the error and the runtime exception.


1 Answers

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.

like image 73
jjmontes Avatar answered Oct 04 '22 04:10

jjmontes