void (PsiElement element, PsiClassType[] exceptions) { if (exceptions.length == 0) return; final GroovyPsiElement context = PsiTreeUtil.getParentOfType(element, GrTryCatchStatement.class, GrClosableBlock.class, GrMethod.class, GroovyFile.class); if (context instanceof GrClosableBlock) { element = generateTryCatch(element, exceptions); } else if (context instanceof GrMethod) { final PsiClassType[] handledExceptions = ((GrMethod)context).getThrowsList().getReferencedTypes(); final List<PsiClassType> psiClassTypes = filterOutExceptions(exceptions, context, handledExceptions); element = generateTryCatch(element, psiClassTypes.toArray(PsiClassType.EMPTY_ARRAY)); } else if (context instanceof GroovyFile) { element = generateTryCatch(element, exceptions); } else if (context instanceof GrTryCatchStatement) { final GrCatchClause[] catchClauses = ((GrTryCatchStatement)context).getCatchClauses(); List<PsiClassType> referencedTypes = ContainerUtil.map(catchClauses, grCatchClause -> { final GrParameter grParameter = grCatchClause.getParameter(); final PsiType type = grParameter != null ? grParameter.getType() : null; if (type instanceof PsiClassType) { return (PsiClassType)type; } else { return null; } }); referencedTypes = ContainerUtil.skipNulls(referencedTypes); final List<PsiClassType> psiClassTypes = filterOutExceptions(exceptions, context, referencedTypes.toArray(PsiClassType.EMPTY_ARRAY)); element = fixCatchBlock((GrTryCatchStatement)context, psiClassTypes.toArray(PsiClassType.EMPTY_ARRAY)); } // CodeStyleManager.getInstance(element.getProject()).reformat(element); }