2017  Kodetalk | Feedback | Privacy Policy | Terms | About

best approach practices to handle exceptions in Spring?

 Can any one brief on best practices in handling exceptions in Spring?

Some of the best practices in handling exceptions in spring:

  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.   Create custom exceptions and throw those instead (you can pass the actual exception as the cause ie throw new MyCustomException("my message", e)).   
  4. Your @ExceptionHandler method can decide what view to return to the user, or you can configure a custom error page in your web.xml

Answer is