java - How to handle exception thrown from ExceptionHandler in controller with ExceptionHandler in ControllerAdvice? -
i have custom exceptions extending exception (myexception1, myexception2, myexception3)
@controller public class mycontroller { /* method throwing myexception1 method throwing myexception2 method throwing myexception3 */ @exceptionhandler(myexception1.class) public void handlemyexception1(exception ex){ //do throw ex; } @exceptionhandler(myexception2.class) public void handlemyexception2(exception ex){ system.out.println("exception logged inside controller") } } @controlleradvice public class myglobalexceptionhandler { @exceptionhandler(exception.class) public void handleallexception(exception ex){ system.out.println("exception logged outside controller"); } }
intention: log myexception1 controller advice log myexception2 inside handler in controller log myexception3 controller advice myexception2 , myexception3 working intended myexception1 fails
"failed invoke @exceptionhandler method .....handlemyexception1"
you can pick 1 of following options exception handling:
option(1) : remove @exceptionhandler(myexception1.class)
method controller automatically handled myglobalexceptionhandler
.
option(2) : create myexception4
(which wrapper myexception1
added information) & throw controller shown below:
@controller public class mycontroller { /* method throwing myexception1 method throwing myexception2 method throwing myexception3 */ @exceptionhandler(myexception1.class) public void handlemyexception1(exception ex){ //myexception4 exe4 = new myexception4(); // add required details throw exe4; } @exceptionhandler(myexception2.class) public void handlemyexception2(exception ex){ system.out.println("exception logged inside controller") } } @controlleradvice public class myglobalexceptionhandler { @exceptionhandler(exception.class) public void handleallexception(exception ex){ system.out.println("exception logged outside controller"); } }
p.s.: did not add option(3) here, manually invoking myglobalexceptionhandler
's handleallexception()
not practice. rather should throw exception , @exceptionhandler
take care automatically.
one more problem manual invocation @ point of time in future, problematic debug exceptions of flows manually call myglobalexceptionhandler
, flows called framework.
Comments
Post a Comment