Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8054569 | 9 | Vicente Arturo Romero Zaldivar | P3 | Closed | Duplicate |
When compiling the following class:
import java.util.Optional;
class Test {
void test() {
Optional<String> s = Optional.ofNullable("string");
String res = s.map(st -> "non-null").orElseThrow(() -> {throw new NullPointerException("Gee, didn't see that coming!");});
}
}
the compiler reports an error like this:
error: unreported exception Throwable; must be caught or declared to be thrown
String res = s.map(st -> "non-null").orElseThrow(() -> {throw new NullPointerException("Gee, didn't see that coming!");});
Because of possible type inference, no compiler error should be reported.
This bug can always be produced.
You can work around this bug, by using an anonymous inner class with explicit generic type, i.e. something like:
[...].orElseThrow(new Supplier< NullPointerException >() {
@Override
public NullPointerException get() {
throw new NullPointerException("Gee, didn't see that coming!");
}
})
Or by explicitly typing orElseThrow() like this:
[...].<NullPointerException>orElseThrow(() -> {throw new NullPointerException("Gee, didn't see that coming!");});
import java.util.Optional;
class Test {
void test() {
Optional<String> s = Optional.ofNullable("string");
String res = s.map(st -> "non-null").orElseThrow(() -> {throw new NullPointerException("Gee, didn't see that coming!");});
}
}
the compiler reports an error like this:
error: unreported exception Throwable; must be caught or declared to be thrown
String res = s.map(st -> "non-null").orElseThrow(() -> {throw new NullPointerException("Gee, didn't see that coming!");});
Because of possible type inference, no compiler error should be reported.
This bug can always be produced.
You can work around this bug, by using an anonymous inner class with explicit generic type, i.e. something like:
[...].orElseThrow(new Supplier< NullPointerException >() {
@Override
public NullPointerException get() {
throw new NullPointerException("Gee, didn't see that coming!");
}
})
Or by explicitly typing orElseThrow() like this:
[...].<NullPointerException>orElseThrow(() -> {throw new NullPointerException("Gee, didn't see that coming!");});
- backported by
-
JDK-8054569 problem with type inference of generic exceptions
- Closed
-
JDK-8056983 Spurious error about generic exception not caught
- Closed