PsiElement (final PsiFile file, final int offset) { PsiElement element = file.findElementAt(offset); if (element instanceof PsiWhiteSpace) element = file.findElementAt(offset - 1); if (element == null) return null; PsiElement parentStatement = CommonJavaRefactoringUtil.getParentStatement(element, false); if (parentStatement instanceof PsiDeclarationStatement) { PsiElement[] declaredElements = ((PsiDeclarationStatement)parentStatement).getDeclaredElements(); if (declaredElements.length > 0 && declaredElements[0] instanceof PsiClass) { return null; } } final PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class, PsiMethod.class, PsiFunctionalExpression.class); if (parent == null || parent instanceof PsiMethod || parent instanceof PsiFunctionalExpression) return null; final PsiTryStatement statement = (PsiTryStatement) parent; final PsiCodeBlock tryBlock = statement.getTryBlock(); if (tryBlock != null) { TextRange range = tryBlock.getTextRange(); if (range.contains(offset) || range.getEndOffset() == offset) { if (!getExceptions(tryBlock, statement.getParent()).isEmpty()) { return tryBlock; } } } final PsiResourceList resourceList = statement.getResourceList(); if (resourceList != null && resourceList.getTextRange().contains(offset)) { if (!getExceptions(resourceList, statement.getParent()).isEmpty()) { return resourceList; } } return null; }