public CompletableFuture<Set<BusStop>> getBusStops() {
CompletableFuture<Set<BusStop>> stops = new CompletableFuture<Set<BusStop>>();
try {
CompletableFuture<Scanner> sc = CompletableFuture.supplyAsync(() ->
new Scanner(BusAPI.getBusStopsServedBy(serviceId).get()));
stops = sc.thenApply(x -> x.useDelimiter("\n")
.tokens()
.map(line -> line.split(","))
.map(fields -> new BusStop(fields[0], fields[1]))
.collect(Collectors.toSet()));
//sc.close();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return stops;
}
I got these errors:
BusService.java:36: error: unreported exception InterruptedException; must be caught or dec
lared to be thrown
new Scanner(BusAPI.getBusStopsServedBy(serviceId).get()));
^
BusService.java:44: error: exception InterruptedException is never thrown in body of corres
ponding try statement
} catch (InterruptedException e) {
^
BusService.java:46: error: exception ExecutionException is never thrown in body of correspo
nding try statement
} catch (ExecutionException e) {
^
3 errors
I am abit confused, since the compiler said exception must be caught, but in the next line it said exception never thrown?
And how should I change the sc.close()
since it is now a CompletableFuture.