In this block of fluent-style code:
getNamesService.getNames(request)
.thenAccept(r -> {
responseObserver.onNext(r);
responseObserver.onCompleted();
})
.exceptionally(t -> {
System.out.println("About to handle the exception");
responseObserver.onError(handleException(t));
});
If this entire block runs, getNamesService.getNames(request)
is called, and the return value from that method is an object that contains a thenAccept
method. That method is then called, and it returns an object that contains an exceptionally
method. Finally, that exceptionally
method is called.
There is no magic here. This is just plain old Python. This chain works because each method call returns an object that contains a method such that the next method call in the chain is valid. If a method in the chain throws an exception instead of returning an object containing the next method in the chain, the rest of the chain isn't executed.
When getNamesService.getNames(request)
is called and throws an exception, the remaining methods in the chain, thenAccept
and exceptionally
, are not executed, and so there's no reason to expect that the System.out.println
in the block will be called.
For the code you supplied, getNamesService.getNames(request)
always throws an exception, and so the complicated code block above is equivalent in function to just:
getNamesService.getNames(request)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…