Question

I need some ideas on how to implement Exceptions in our product.

I think of few ideas like: 1.) Since we are using Spring AOP, can i create some aspect,advice around catch block so that
i do not have to change everywhere in the code. Also Spring global exception handler is
there which only comes into play if no catch block is there. 2.) Auditing exceptions in database, generating reports so as o control and make changes in the code accordingly. 3.) Sending emails to specific groups. 4.) Displaying different error pages, handling different error codes for different exceptions. 5.) Fine gaining exception stacktrace to extract use full information and display to business
users/end users.

Please share what else we can provide as apart of exception modelling in our product. Any avilable API we can consult..???

Was it helpful?

Solution

Group the exceptions into several groups by creating one top level exception per group, and then create subclasses.

These are the examples of some exceptions: BusinessException, a subclass ValidationException, FatalException, etc. For each group of exceptions define a global catch mechanism using a @ControllerAdvice annotatted class, that configures exception handlers for all controllers.

See this blog post for an example of how to create a ControllerAdvice class.

Then in the service layer its possible to catch the exception and do something specific if needed, and then rethrow the exception add still trigger the remote behaviour.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top