PsiCodeBlock (@NotNull PsiElement element, @NotNull PsiType thrownType) { PsiElement currentElement = element; while (true) { final PsiTryStatement tryStatement = PsiTreeUtil .getParentOfType(currentElement, PsiTryStatement.class, true, PsiClass.class, PsiLambdaExpression.class); if (tryStatement == null) { return null; } final PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock(); if (PsiTreeUtil.isAncestor(finallyBlock, currentElement, true)) { return finallyBlock; } if (PsiTreeUtil.isAncestor(tryStatement.getTryBlock(), currentElement, true) && isCaught(tryStatement, thrownType)) { return null; } currentElement = tryStatement; } }