The following code uses try-with-resources , created in Java 8. occasionallyThrow () is declared to throw an OccasionalException , the Resource method close () to throw a CloseException . Eclipse (Version: Neon Release (4.6.0), Build id: 20160613-1800) adds a warning to the line marked with // dead code that the branch is dead code. Implicitly, Eclipse claims that the line marked with // live code is not dead code.
Object tryWithResources() throws OccasionalException {
Object value = null;
try (Resource resource = new Resource()) {
occasionallyThrow();
value = new Object();
}
catch (CloseException e) {
if (value == null) {
}
else {
}
}
return value;
}
. Throw() OccasionalException, try -with-resources . CloseException, OccasionalException, CloseException. , , CloseException, catch try , , null. , " " , " " . , , , , , , " " .
, , , try-with-resources, . ( , , 14.20.3.2. try-with-resources, , t , & hellip;)
Object expandedTry() throws OccasionalException {
Object value = null;
try {
Resource resource = new Resource();
Throwable $primary = null;
try {
occasionallyThrow();
value = new Object();
}
catch (Throwable t) {
$primary = t;
throw t;
}
finally {
if (resource != null) {
if ($primary != null) {
try {
resource.close();
}
catch (Throwable $suppressed) {
$primary.addSuppressed($suppressed);
}
}
else {
resource.close();
}
}
}
}
catch (CloseException e) {
if (value == null) {
}
else {
}
}
return value;
}
-, if-else , ?
, .
public class TestTryWithResources {
@SuppressWarnings("serial")
static class CloseException extends Exception {}
static class Resource implements AutoCloseable {
@Override
public void close() throws CloseException {}
}
@SuppressWarnings("serial")
static class OccasionalException extends Exception {}
void occasionallyThrow() throws OccasionalException {}
Object tryWithResources() throws OccasionalException {
Object value = null;
try (Resource resource = new Resource()) {
occasionallyThrow();
value = new Object();
}
catch (CloseException e) {
if (value == null) {
}
else {
}
}
return value;
}
Object expandedTry() throws OccasionalException {
Object value = null;
try {
Resource resource = new Resource();
Throwable $primary = null;
try {
occasionallyThrow();
value = new Object();
}
catch (Throwable t) {
$primary = t;
throw t;
}
finally {
if (resource != null) {
if ($primary != null) {
try {
resource.close();
}
catch (Throwable $suppressed) {
$primary.addSuppressed($suppressed);
}
}
else {
resource.close();
}
}
}
}
catch (CloseException e) {
if (value == null) {
}
else {
}
}
return value;
}
}
Amin J Eclipse. . , , , Luna, Neon:

