OpenFeign/feign · error · IllegalStateException

Cannot instantiate exception with constructor

Error message

Cannot instantiate exception with constructor

What it means

Thrown by MethodErrorHandler.createException when InstantiationException is raised while reflectively instantiating the mapped exception class. Typically the class is abstract, an interface, or has no concrete instantiable constructor, so newInstance() cannot create it.

Solutions

  1. Map a concrete, non-abstract exception class in @ErrorHandling/@ErrorCodes generate()
  2. Create a concrete subclass for the abstract exception and reference that
  3. Verify the class is not an interface or anonymous class

Example fix

// before
@ErrorHandling(codeSpecific = @ErrorCodes(codes = {500}, generate = AbstractApiException.class))
// after
@ErrorHandling(codeSpecific = @ErrorCodes(codes = {500}, generate = ConcreteApiException.class))
Defensive patterns

Strategy: validation

Validate before calling

if (Modifier.isAbstract(ApiError.class.getModifiers()) || ApiError.class.isInterface()) {
  throw new IllegalStateException("generate= must reference a concrete exception class");
}

Type guard

boolean isConcreteException = !Modifier.isAbstract(cls.getModifiers()) && Throwable.class.isAssignableFrom(cls);

Try / catch

try { api.call(); } catch (IllegalStateException e) {
  if ("Cannot instantiate exception with constructor".equals(e.getMessage())) { ... }
}

Prevention

When it happens

Trigger: Mapping an abstract exception base class or interface in @ErrorHandling(generate=...); mapping a class whose @FeignExceptionConstructor constructor resolution ends at an abstract type; anonymous/abstract classes configured as the generate target.

Common situations: Pointing errorHandling at a shared abstract ApiException parent instead of a concrete subclass; refactoring the concrete exception to abstract after introducing subclasses.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of OpenFeign/feign@e2a1e27560 (2026-09-10). Data as JSON: /api/errors/f0ace90e273bcaed. Report an issue: GitHub.

Appendix: source

Thrown at annotation-error-decoder/src/main/java/feign/error/MethodErrorHandler.java:58

  }

  private ExceptionGenerator getConstructorDefinition(Response response) {
    if (methodLevelExceptionsByCode.containsKey(response.status())) {
      return methodLevelExceptionsByCode.get(response.status());
    }
    if (classLevelExceptionsByCode.containsKey(response.status())) {
      return classLevelExceptionsByCode.get(response.status());
    }
    return defaultException;
  }

  protected Exception createException(ExceptionGenerator constructorDefinition, Response response) {
    try {
      return constructorDefinition.createException(response);
    } catch (IllegalAccessException e) {
      throw new IllegalStateException("Cannot access constructor", e);
    } catch (InstantiationException e) {
      throw new IllegalStateException("Cannot instantiate exception with constructor", e);
    } catch (InvocationTargetException e) {
      throw new IllegalStateException("Cannot invoke constructor", e);
    } catch (NoSuchMethodException e) {
      throw new IllegalStateException("Constructor does not exist", e);
    }
  }
}

View on GitHub (pinned to e2a1e27560)