Refactoring code to throw a single exception per function One of my functions must throws 2 exceptions, but I understand 1 exception per function is preferred (at least according to my client's Sonar ruleset).  Can you suggest how I might refactor the following code to enable 1 or no exceptions on these functions?

Worth noting is that I have a global exception handler which wraps this class, so any thrown exception will be caught there.

public ResponseEntity postFooRequest(
        @RequestBody String body,
        @RequestParam(value = "action") String action,
        @RequestParam(value = "fooNumber") String fooNumber) throws UnsupportedEncodingException, InterruptedException {
    String sanitizedBody = inputSanitizer.sanitizeBody(body);
    FooRequest fooRequest = new FooRequest(sanitizedBody, action, fooNumber);
    return processFooRequest(fooRequest);
}

private ResponseEntity processFooRequest(FooRequest fooRequest) throws InterruptedException, UnsupportedEncodingException {
    FooRequest fooRequestResult = fooRequestDelegate.processFooRequest(fooRequest);
    return new ResponseEntity&lt;&gt;(decodeResponse(fooRequestResult.getBody()), httpHeaders, HttpStatus.OK);
}

private String decodeResponse(String body) throws UnsupportedEncodingException {
    return java.net.URLDecoder.decode(body, "UTF-8");
}
UnsupportedEncodingException can easily be avoided by wrapping the call that might throw it:

try {
  return java.net.URLDecoder.decode(body, "UTF-8");
} catch (UnsupportedEncodingException ex) {
  throw new IllegalStateException(ex);
}


Indeed, "UTF-8" is one of most common encodings and that would be really surprising that such exception is thrown here. Semantically, it is close to an illegal state of the application: one would hardly imagine that it cannot support this encoding nowadays.

Since the chained IllegalStateException is a RuntimeException, you will be able to remove it from the signatures of the methods.