1

Although not explicitly specified in @ExceptionHandler's official documentation, almost in all the examples on web, methods annotated with @ExceptionHandler are protected. Is there a specific reason for that?

canan
  • 63
  • 1
  • 7
  • Why should any code use such a class directly? Spring takes care to register the exception handler. That's all there is to do. –  May 23 '19 at 10:52
  • 1
    I wonder if there used to a class you would need to extend before `@ExceptionHandler` annotation came along – Eugene May 23 '19 at 10:53
  • @LutzHorn not should but might. So can we say that's more to force cleaner design? – canan May 23 '19 at 13:31

1 Answers1

1

If I am not wrong this is because the methods annotated with @ExceptionHandler annotation are only active for that particular Controller, not globally for the entire application thus making them protected.

Majid Ali Khan
  • 701
  • 8
  • 13
  • Can it be the opposite? I mean the method should be public when that annotation is used at Controller level. @ControllerAdvice implementations are almost always protected. – canan May 23 '19 at 13:34
  • That being said, we can work around even without making them public all we have to do is having all Controllers extend a Base Controller class. – Majid Ali Khan May 24 '19 at 07:43