IOException
Cyclops React の「Try with Resources」ブロックで、カスタム例外タイプにマップしたいと思います。私はこれをJavaslangでも試しましたが、すべての例外を同じように扱うため、柔軟性が低いようです。
コード例:
private static Try<String, ConnectionError> readString() {
// Socket is a thread-local static field
final Try<String, IOException> string = Try.catchExceptions(IOException.class)
.init(() -> new BufferedReader(new InputStreamReader(socket.get().getInputStream())))
.tryWithResources(BufferedReader::readLine);
return string.isSuccess() ? Try.success(string.get()) :
Try.failure(new ConnectionError("Could not read from server", string.failureGet()));
}
これはよりエレガントな方法で行うことができますか? それとも意味がなく、戻ったほうがいいTry<String, IOException>
ですか?
免責事項: 私は Cyclops React ライブラリと関数型プログラミング全般を使用する初心者であるため、深刻な概念上の誤解がある可能性があります。