8

I have been developing a web app in Spring + hibernate for last few months. What I have been seriously missing is exception handling.

I want to know the best approaches and Practices to handle exception? I have some questions in my mind which could not be covering all aspects of exception handling like:

1.Whether to make checked or unchecked Exception?How to decide?

2.How to handle and what to do with the exceptions generated in the Controller.

3.What about exceptions which are generated in Service layer and DAO layer?should they be handled in that layer only or should it be transferred to controller layer?

4.Since there are could be numerous exceptions,how could I be prepared to handle those which could come in future?

5.How can I display relevant messages to UI or browser?

Please suggest or provide links to good blogs?

beinghuman
  • 2,088
  • 7
  • 27
  • 36

2 Answers2

12
  1. Use Checked Exceptions if there is a reasonable expectation that the client can handle and recover from the exception otherwise use Unchecked (you will mostly use unchecked).
  2. Use the @ExceptionHandler annotation on a method to handle exceptions generated from @RequestMapping methods.
  3. throw them to the controller so it can decide the best response, unless the service method can actually recover from the exception and proceed with processing as normal.
  4. Create custom exceptions and throw those instead (you can pass the actual exception as the cause ie throw new MyCustomException("my message", e))
  5. Your @ExceptionHandler method can decide what view to return to the user, or you can configure a custom error page in your web.xml
jax
  • 37,735
  • 57
  • 182
  • 278
  • thanks for the explanation.your suggestions are very good could you provide some specific examples?may be some links. – beinghuman Aug 29 '13 at 12:47
5
  1. In general don't use checked exceptions, you don't want to use exception for flow control, next to that most exceptions are unrecoverable so then what are you going to do? See this related answer.

  2. Regarding exception handling, spring has the HandlerExceptionResolver for that. You can use this for general exception handling. For more fine grained control you can annotate methods in your controller with @ExceptionHandler those will handle exceptions occuring from that controller only. (Although with the new @ControllerAdvice you can also register global @ExceptionHandler methods.

  3. In some cases you might want to catch the exception in your controller and handle them inside the controller (i.e. show a warning to the user for instance).

As a last resort you can always configure the error-page in your web.xml for very generic and broad exception handling.

For more information you might want to check the exception handling section in the Spring Reference Guide.

Community
  • 1
  • 1
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • hello again.These advices are very good but actually i am very weak in exception handling.May I need to revise concepts?any suggestions for me? – beinghuman Aug 29 '13 at 12:49