precise rethrow: ensure unreachable catch sections do not participate in precise rethrow

This commit is contained in:
Anna.Kozlova
2017-03-08 12:37:00 +01:00
parent 0fa0b96dc6
commit 7f4059541a
2 changed files with 21 additions and 4 deletions
@@ -34,6 +34,7 @@ import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -123,12 +124,16 @@ public class PsiCatchSectionImpl extends CompositePsiElement implements PsiCatch
}
return thrownType;
});
if (uncaughtTypes.isEmpty()) return Collections.emptyList(); // unreachable catch section
// ... and T is assignable to Ej ...
// ... the throw statement throws precisely the set of exception types T.
if (uncaughtTypes.stream().anyMatch(type -> declaredType.isAssignableFrom(type) ||
type instanceof PsiClassType && ExceptionUtil.isUncheckedException((PsiClassType)type))) {
return uncaughtTypes;
List<PsiType> types = new ArrayList<>();
for (PsiType type : uncaughtTypes) {
if (declaredType.isAssignableFrom(type) || type instanceof PsiClassType && ExceptionUtil.isUncheckedException((PsiClassType)type)) {
types.add(type);
}
}
// ... the throw statement throws precisely the set of exception types T.
if (!types.isEmpty()) return types;
}
return Collections.singletonList(declaredType);
@@ -250,4 +250,16 @@ class C {
if (i == 1) throw new E1();
if (i == 2) throw new E2();
}
private void m15() throws E1 {
try {
throw new E1();
}
catch (final E1 e1) {
throw e1;
}
catch (final E e) {
throw e;
}
}
}