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
- Map a concrete, non-abstract exception class in @ErrorHandling/@ErrorCodes generate()
- Create a concrete subclass for the abstract exception and reference that
- 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
- Never map abstract base classes or interfaces in @ErrorCodes/@ErrorHandling generate()
- Create concrete subclasses per error scenario
- Keep exception classes non-abstract, public, with an invokable constructor
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
- Cannot generate exception - check constructor parameter…
- Too many constructors marked with @FeignExceptionConstructor
- Cannot find any suitable constructor in class
- Cannot access constructor
- Cannot invoke constructor
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)