FULL PRODUCT VERSION :
java version "1.9.0-ea"
Java(TM) SE Runtime Environment (build 1.9.0-ea-b82)
Java HotSpot(TM) 64-Bit Server VM (build 1.9.0-ea-b82, mixed mode)
also
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Mac OS X 10.10.5 Yosemite
Darwin Kernel Version 14.5.0
EXTRA RELEVANT SYSTEM CONFIGURATION :
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.9.0.jdk/Contents/Home
A DESCRIPTION OF THE PROBLEM :
When throwing an exception from Optional.orElseThrow(), the compiler fails to infer that a unchecked exception is thrown unless the Optional's element type T is specified.
Possibly related toJDK-8054569, but since it's still present in the latest 1.9 release (b82) I think this is separate.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Create a file called Test.java and paste in the supplied source code.
2) Run "javac Test.java"
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Clean compilation with no errors and output of Test.class to the same directory.
ACTUAL -
Compilation failed with an error, requiring the exception to be caught or declared on the method, despite it being a RuntimeException.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Test.java:12: error: unreported exception Throwable; must be caught or declared to be thrown
Object value3 = opt3.orElseThrow(() -> new RuntimeException("Test Exception"));
^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
// Test.java
import java.util.Optional;
public class Test {
public static void main(String[] args) {
Optional<Object> opt1 = Optional.of("foo");
// fine
Object value1 = opt1.orElseThrow(() -> new RuntimeException("Test Exception"));
Optional<?> opt2 = opt1;
// also fine
Object value2 = opt2.orElseThrow(() -> new RuntimeException("Test Exception"));
Optional opt3 = opt1;
// compiler error
Object value3 = opt3.orElseThrow(() -> new RuntimeException("Test Exception"));
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Supply the element type for the Optional, either with a concrete type or a wildcard.
java version "1.9.0-ea"
Java(TM) SE Runtime Environment (build 1.9.0-ea-b82)
Java HotSpot(TM) 64-Bit Server VM (build 1.9.0-ea-b82, mixed mode)
also
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Mac OS X 10.10.5 Yosemite
Darwin Kernel Version 14.5.0
EXTRA RELEVANT SYSTEM CONFIGURATION :
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.9.0.jdk/Contents/Home
A DESCRIPTION OF THE PROBLEM :
When throwing an exception from Optional.orElseThrow(), the compiler fails to infer that a unchecked exception is thrown unless the Optional's element type T is specified.
Possibly related to
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Create a file called Test.java and paste in the supplied source code.
2) Run "javac Test.java"
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Clean compilation with no errors and output of Test.class to the same directory.
ACTUAL -
Compilation failed with an error, requiring the exception to be caught or declared on the method, despite it being a RuntimeException.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Test.java:12: error: unreported exception Throwable; must be caught or declared to be thrown
Object value3 = opt3.orElseThrow(() -> new RuntimeException("Test Exception"));
^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
// Test.java
import java.util.Optional;
public class Test {
public static void main(String[] args) {
Optional<Object> opt1 = Optional.of("foo");
// fine
Object value1 = opt1.orElseThrow(() -> new RuntimeException("Test Exception"));
Optional<?> opt2 = opt1;
// also fine
Object value2 = opt2.orElseThrow(() -> new RuntimeException("Test Exception"));
Optional opt3 = opt1;
// compiler error
Object value3 = opt3.orElseThrow(() -> new RuntimeException("Test Exception"));
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Supply the element type for the Optional, either with a concrete type or a wildcard.