"2. Create your own exception class," but not for everyone. Complete exceptions in logical groups. For example, you can have an XmlWritingException , PaymentGatewayException and DataAccessException that throw different exceptions depending on the scenario.
It is even possible (and preferably) to wrap the same exception in a different shell. For example, you can wrap an IOException in a PaymentGatewayException if the payment was unsuccessful due to a communication problem, but in an XmlWritingException if it failed during some xml I / O. All of these examples are hypothetical, but you get the idea.
The most important thing is to establish the original exception as the cause of the new one so that it is not lost.
Update: In fact, option 5 is great if you cannot expect the client to recover normally from the exception. Even better, the custom exceptions you create can be extended by a RuntimeException . What spring does, for example, wrap all data-related exceptions in a DataAccessException .
Bozho
source share