Understanding behavior of `exceptionally()`

Hello,

I am reading through the example of searching for CatalogItems using which is presented here.
The very last call sends the prepared SearchCatalogItemsRequest instance to the Square API, as follows:

catalogApi.searchCatalogItemsAsync(body).thenAccept(result -> {
    // TODO success callback handler
}).exceptionally(exception -> {
    // TODO failure callback handler
    return null;
});

I wanted to ask: if the search comes up empty, but does not fail for some other reason, will the exceptionally method be applied, or not? The relevant Oracle JDK page for CompletionStage is not necessarily illuminating: what do they mean by “when this stage completes exceptionally”? Is it guaranteed that an empty search is not covered by exceptionally()?

As far as I understand it, an empty response is not the same as an error response, and thus it should go in the thenAccept (not exceptionally). The response body would just be an empty array/object, though.

1 Like