Java: even more precise rethrow types (IJ-CR-13170)

GitOrigin-RevId: 34d767e800b3dc24d11fa05e6d237b88b630cb0c
This commit is contained in:
Bas Leijdekkers
2021-08-23 13:50:58 +00:00
committed by intellij-monorepo-bot
parent 6f7d008170
commit 333c564c62
2 changed files with 23 additions and 3 deletions
@@ -131,7 +131,10 @@ public class PsiCatchSectionImpl extends CompositePsiElement implements PsiCatch
private static List<PsiType> computePreciseCatchTypes(PsiType declaredType, List<PsiType> uncaughtTypes) {
List<PsiType> types = new SmartList<>();
for (PsiType type : uncaughtTypes) {
if (declaredType.isAssignableFrom(type) ||
if (type.isAssignableFrom(declaredType)) {
types.add(declaredType);
}
else if (declaredType.isAssignableFrom(type) ||
// JLS 11.2.3 "Exception Checking":
// "It is a compile-time error if a catch clause can catch checked exception class E1 and it is not the case
// that the try block corresponding to the catch clause can throw a checked exception class that is
@@ -142,8 +145,7 @@ public class PsiCatchSectionImpl extends CompositePsiElement implements PsiCatch
}
}
// ... the throw statement throws precisely the set of exception types T.
if (!types.isEmpty()) return Collections.unmodifiableList(types);
return Collections.singletonList(declaredType);
return types;
}
private static Collection<PsiClassType> getThrownTypes(@NotNull PsiTryStatement statement) {
@@ -277,4 +277,22 @@ class C {
}
catch (E ignored) {}
}
private void m17() throws InterruptedException, java.io.FileNotFoundException {
try {
Thread.sleep(1);
throw new java.io.FileNotFoundException();
} catch (InterruptedException | java.io.IOException ex) {
throw ex;
}
}
private void m18() throws InterruptedException {
try {
Thread.sleep(1L);
// no IOException thrown
} catch (<error descr="Exception 'java.io.IOException' is never thrown in the corresponding try block">java.io.IOException ex</error>) {
throw ex;
}
}
}