title: Using Spring Boot's ErrorController and Spring's ResponseEntityExceptionHandler correctly
body: <h2>The Question</h2>
  
  <p>When creating a controller in Spring Boot to handle <strong>all</strong> errors/exceptions in a custom way, <strong>including custom exceptions</strong>, which technique should be preferred?</p>
  
  <ol>
  <li><p>Should the controller implement the Spring Boot's <code>ErrorController</code>?</p></li>
  <li><p>Should the controller extend Spring's <code>ResponseEntityExceptionHandler</code>?</p></li>
  <li><p>Both: a single controller implementing and extending both classes including both of their functionality?</p></li>
  <li><p>Both: two separate controllers, one implementing <code>ErrorController</code>, the other extending <code>ResponseEntityExceptionHandler</code>?</p></li>
  </ol>
  
  <h2>The Goal</h2>
  
  <p>The reason for this post is to find a way of exception-handling in Spring Boot with <em>all</em> of the following attributes:</p>
  
  <ul>
  <li>All <code>Throwable</code>s occurring in controllers/filters/interceptors during the handling of a request should be caught.</li>
  <li>In the case of catching a <code>Throwable</code>, we <strong>do not</strong> want to expose any of the stack trace or other implementation details to the client <strong>ever</strong> (unless explicitly coded that way).</li>
  <li>It should be possible to handle all occurred <code>Throwable</code>s separately by their class. For any other non-specified type, a default response can be specified. (I know for sure, that this <em>is</em> possible with <code>@ExceptionHandler</code>. But <code>ErrorController</code>?)</li>
  <li>The code should be as clean and explicit as possible with no ugly work-arounds or UB to achieve this goal.</li>
  </ul>
  
  <h2>More Details</h2>
  
  <p>I noticed that both the controllers (see 1 and 2 above) may contain methods returning a <code>ResponseEntity</code> object, thus handling the occurred exception and returning a response to the client. So they could in theory produce the same result?</p>
  
  <p>There are several tutorials out there on the use of techniques 1 and 2. But I have found no articles considering both options, comparing them or using them together, which raises several additional questions:</p>
  
  <ol>
  <li><p>Should they even be considered together?</p></li>
  <li><p>What are the main differences between these two proposed techniques? What are the similarities?</p></li>
  <li><p>Is one a more powerful version of the other? Is there something one can do that the other can't and vice versa?</p></li>
  <li><p>Can they be used together? Are there situations where this would be necessary?</p></li>
  <li><p>If they are used together, how would an exception be handled? Does it go through both handlers or just one? In the case of the latter, which one?</p></li>
  <li><p>If they are used together, and an exception is thrown <em>inside</em> the controller (one or the other) <strong>during exception handling</strong>, how would that exception be handled? Is it sent to the other controller? Could exceptions theoretically start bouncing between controllers or create some other kind of non-recovering loop?</p></li>
  <li><p>Is there any trusted/official documentation on how Spring Boot uses Spring's <code>ResponseEntityExceptionHandler</code> internally or how it expects it to be used in Spring Boot applications?</p></li>
  <li><p>If <code>ResponseEntityExceptionHandler</code> alone is already enough, then why does <code>ErrorController</code> exist?</p></li>
  </ol>
  
  <p>When looking at the Spring's <code>ResponseEntityExceptionHandler</code> together with the <code>@ExceptionHandler</code> annotation, it seems to be more powerful in handling different types of exceptions separately and using cleaner code. But because Spring Boot is built on top of Spring, does this mean:</p>
  
  <ul>
  <li>When using Spring Boot, we should implement <code>ErrorController</code> instead of extending <code>ResponseEntityExceptionHandler</code>?</li>
  <li>Can <code>ErrorController</code> do everything <code>ResponseEntityExceptionHandler</code> can, including handling different types of exceptions separately?</li>
  </ul>
  
  <p><strong>EDIT:</strong> Related: <a href="https://stackoverflow.com/q/40489606/2237467">Spring @ControllerAdvice vs ErrorController</a></p>