How Runtime Exceptions are handled by Spring ExceptionHandler -
i using spring's @controlleradvice , @exceptionhandler exception handling. method throws custom exception controller , corresponding @exceptionhandler handle it. if runtime exception occurs(eg hibernateexception) throw runtime exception , dont have @exceptionhandler runtimeexceptions.
my question how handle runtime exception? need add @exceptionhandler every exception thrown controller? dont want create generic exceptionhandler exception.class because have send different error code according exception occured.
one way add try catch block in controller , throw custom exception catch block? or there better way?
all @exceptionhandlers inside @controlleradvice class.
the alternative don't catch exception
in controller
. catch exception
in service
layer , throw custom exception eg. if persisting record failed, throw databaseexception
message. see below method:
student persist(student object){ try{ studentdao.insert(object); }catch(hibernateexception e){ throw new databaseexception("database operation failed!"); } return student; }
from exception handler method can message. way can set different message on different exception.
Comments
Post a Comment