Loom Evades Exception Handling
Eric Kolotyluk
eric at kolotyluk.net
Thu Nov 25 20:27:04 UTC 2021
Yes, that subject line is click-bait, but this is not intuitive to me... Am
I just seeing an incomplete implementation of Project Loom, and this will
be resolved in the future?
public class Experiment06 {
public static void main(String args[]) {
Context.printHeader(Experiment06.class);
try {
getRemoteStrings().forEach(System.out::println);
} catch (ExperimentException e) {
e.printStackTrace();
}
}
static Stream<String> getRemoteStrings() throws ExperimentException {
try (var structuredExecutor = StructuredExecutor.open("Experiment06")) {
// We want complete results, so we won't tolerate failure.
var completionHandler = new StructuredExecutor.ShutdownOnFailure();
var futureResults = IntStream.range(0, 15).mapToObj(item -> {
try {
System.out.printf("item = %d, Thread ID = %s\n",
item, Thread.currentThread());
return structuredExecutor.fork(() -> {
try {
System.out.printf("\ttask = %d, Thread ID
= %s\n", item, Thread.currentThread());
return getRemoteString(item, new
URI("https://server1/foobar.com/item"));
}
catch (Throwable t) {
System.out.printf("TASK EXCEPTION
%s\n\t%s\n\n", t.getMessage(), t.getCause());
t.printStackTrace();
throw t;
}
}, completionHandler);
} catch (Throwable t) {
System.out.printf("SPAWN EXCEPTION %s\n\t%s\n\n",
t.getMessage(), t.getCause());
t.printStackTrace();
throw t;
}
});
structuredExecutor.joinUntil(Instant.now().plusSeconds(10));
completionHandler.throwIfFailed();
return futureResults.map(Future::resultNow);
}
catch (InterruptedException e) {
e.printStackTrace();
throw new ExperimentException(e);
} catch (ExecutionException e) {
e.printStackTrace();
throw new ExperimentException(e);
} catch (TimeoutException e) {
e.printStackTrace();
throw new ExperimentException(e);
} catch (IllegalStateException e) {
e.printStackTrace();
throw new ExperimentException(e);
}
catch (Throwable t) {
t.printStackTrace();
throw new ExperimentException(t);
}
}
static String getRemoteString(int item, URI from) {
return "Item %d from %s".formatted(item, from);
}
static class ExperimentException extends Exception {
ExperimentException(Throwable cause) {
super(cause);
}
}
}
where I get
[image: image.png]
(Experiment06.java:50) is
return structuredExecutor.fork(() -> {
(Experiment06.java:34) is
getRemoteStrings().forEach(System.out::println);
Is there some way I can actually find out more from the Exception handling,
or other techniques?
Cheers, Eric
More information about the loom-dev
mailing list