title: Can we directly throw ResponseStatusException from service layer without throwing custom exception and handling at the controller level?
body: <p>Spring 5 has introduced ResponseStatusException, is it a good practice to throw this exception directly from the service layer.</p>
  
  <p><strong>Case 1:</strong></p>
  
  <pre><code>@Service
  public class UserService {
      public User findUserByName(String username) {
         User user = userRepository.findByUsernName(username);
         if(null == user) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "user not found");
         }
      }
  }
  </code></pre>
  
  <p><strong>Case 2:</strong>
  Or do we need to use custom exception and handle it in controller level ? We are catching the CustomException and throwing ResponseStatusException in this case, why do we have to catch the custom exception again instead of going with <code>Case 1</code></p>
  
  <pre><code>@Service
  public class UserService {
      public User findUserByName(String username) {
         User user = userRepository.findByUsernName(username);
         if(null == user) {
            throw new UserNotFoundException("user not found");
         }
      }
  }
  
  @RestController
  public class UserController {
  
      @GetMapping(path="/get-user")
      public ResponseEntity&lt;User&gt; getUser(String username) {
        try {
           userService.findUserByName(username);
        } catch (UserNotFoundException ex) {
           throw new ResponseStatusException(HttpStatus.NOT_FOUND, "user not found");
        }
      }
  }
  </code></pre>