永远不会在相应的 try 语句体中抛出异常 XXX
exception XXX is never thrown in body of corresponding try statement
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;
}
我遇到了这些错误:
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
我有点困惑,因为编译器说必须捕获异常,但在下一行却说从未抛出异常?
我应该如何更改 sc.close()
因为它现在是 CompletableFuture.
您的第一个 lambda 中的扫描程序可能会抛出异常,您必须在 lambda 中捕获该异常。
所以你的困惑可能是围绕这个上下文:
- 有一次你在 lambda 中,这是一个匿名的 class。在这里你必须捕获异常。
- 其他时候您在您的方法周围 class。这里没有抛出 InterruptedException。
可以像这样在 lambda 中捕获异常:
CompletableFuture<Scanner> sc = CompletableFuture.supplyAsync(() -> {
try {
return new Scanner(...);
} catch (Exception e) {
// Handle Exception
}
});
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;
}
我遇到了这些错误:
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
我有点困惑,因为编译器说必须捕获异常,但在下一行却说从未抛出异常?
我应该如何更改 sc.close()
因为它现在是 CompletableFuture.
您的第一个 lambda 中的扫描程序可能会抛出异常,您必须在 lambda 中捕获该异常。
所以你的困惑可能是围绕这个上下文:
- 有一次你在 lambda 中,这是一个匿名的 class。在这里你必须捕获异常。
- 其他时候您在您的方法周围 class。这里没有抛出 InterruptedException。
可以像这样在 lambda 中捕获异常:
CompletableFuture<Scanner> sc = CompletableFuture.supplyAsync(() -> {
try {
return new Scanner(...);
} catch (Exception e) {
// Handle Exception
}
});