Java: Don't report the catch section as unreachable where a subclass of declared exception can be thrown (IDEA-175863)

This commit is contained in:
Pavel Dolgov
2017-07-18 14:25:32 +03:00
parent 1a50059f81
commit 7d30d532c6
3 changed files with 26 additions and 1 deletions
@@ -1387,7 +1387,8 @@ public class HighlightUtil extends HighlightUtilBase {
if (ExceptionUtil.isGeneralExceptionType(catchType)) continue;
// collect exceptions which are caught by this type
Collection<PsiClassType> caught = ContainerUtil.findAll(thrownTypes, catchType::isAssignableFrom);
final Collection<PsiClassType> caught =
ContainerUtil.findAll(thrownTypes, type -> catchType.isAssignableFrom(type) || type.isAssignableFrom(catchType));
if (caught.isEmpty()) continue;
final Collection<PsiClassType> caughtCopy = ContainerUtil.newHashSet(caught);
@@ -0,0 +1,23 @@
class C {
void foo(boolean b) throws Exception {
try {
if (b) {
throw new ChildException();
} else {
method();
}
} catch (ChildException e) {
System.out.println("child");
} catch (ParentException e) {
System.out.println("parent");
}
}
private static void method() throws Exception {
throw new ParentException();
}
static class ParentException extends Exception { }
static class ChildException extends ParentException { }
}
@@ -186,6 +186,7 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase {
public void testIDEA138978() { doTest(false, false); }
public void testIntersectionTypeCast() { doTest(false, false); }
public void testUsedMethodCalledViaReflectionInTheSameFile() { doTest(true, false); }
public void testCatchSubclassOfThrownException() { doTest(true, false); }
public void testArrayInitializerTypeCheckVariableType() { doTest(false, false);}