private static void addException(@NotNull PsiMethod method,
                                   @Nullable PsiClass exceptionClass,
                                   @Nullable String exceptionName) throws IncorrectOperationException {
    if (exceptionClass == null && exceptionName == null) {
      throw new IllegalArgumentException("One of exceptionName, exceptionClass must be not null");
    }
    PsiReferenceList throwsList = method.getThrowsList();
    PsiJavaCodeReferenceElement[] refs = throwsList.getReferenceElements();
    boolean replaced = false;
    for (PsiJavaCodeReferenceElement ref : refs) {
      if (exceptionClass != null && ref.isReferenceTo(exceptionClass)) return;
      PsiClass aClass = (PsiClass)ref.resolve();
      if (exceptionClass == null || aClass == null) {
        continue;
      }
      if (aClass.isInheritor(exceptionClass, true)) {
        if (replaced) {
          ref.delete();
        }
        else {
          PsiElementFactory factory = JavaPsiFacade.getElementFactory(method.getProject());
          PsiJavaCodeReferenceElement ref1;
          if (exceptionName != null) {
            ref1 = factory.createReferenceElementByFQClassName(exceptionName, method.getResolveScope());
          }
          else {
            PsiClassType type = factory.createType(exceptionClass);
            ref1 = factory.createReferenceElementByType(type);
          }
          ref.replace(ref1);
          replaced = true;
        }
      }
      else if (exceptionClass.isInheritor(aClass, true)) {
        return;
      }
    }
    if (replaced) return;

    PsiElementFactory factory = JavaPsiFacade.getElementFactory(method.getProject());
    PsiJavaCodeReferenceElement ref;
    if (exceptionName != null) {
      ref = factory.createReferenceElementByFQClassName(exceptionName, method.getResolveScope());
    }
    else {
      PsiClass superClass = exceptionClass.getSuperClass();
      while (superClass != null && isLocalOrAnonymousClass(superClass)) {
        superClass = superClass.getSuperClass();
      }
      PsiClassType type = factory.createType(superClass != null ? superClass : exceptionClass);
      ref = factory.createReferenceElementByType(type);
    }
    throwsList.add(ref);
  }
private static void addException(@NotNull PsiMethod method,
                                   @Nullable PsiClass exceptionClass,
                                   @Nullable String exceptionName)