= sftp.getFiles("test.csv"); if (files.isEmpty()) { throw new RuntimeException("No file found"); } String csvContent; try { csvContent = CSVParser.parse(files.get(0)); } catch (IOException e) { throw new RuntimeException("CSV parse failed", e); } return new XXXReader(csvContent); return Try.of(() -> sftp.getFiles("test.csv") ) .filter( f -> !f.isEmpty(), f -> new RuntimeException("No file found") ) .mapTry( f -> CSVParser.parse(f.get(0)) ) .recoverWith( IOException.class, e -> Try.failure( new RuntimeException("CSV parse failed", e)) ) .map(XXXReader::new) .get();