{"record":{"id":"f0ace90e273bcaed","repo":"OpenFeign/feign","slug":"cannot-instantiate-exception-with-constructor","errorCode":null,"errorMessage":"Cannot instantiate exception with constructor","messagePattern":"Cannot instantiate exception with constructor","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"annotation-error-decoder/src/main/java/feign/error/MethodErrorHandler.java","lineNumber":58,"sourceCode":"  }\n\n  private ExceptionGenerator getConstructorDefinition(Response response) {\n    if (methodLevelExceptionsByCode.containsKey(response.status())) {\n      return methodLevelExceptionsByCode.get(response.status());\n    }\n    if (classLevelExceptionsByCode.containsKey(response.status())) {\n      return classLevelExceptionsByCode.get(response.status());\n    }\n    return defaultException;\n  }\n\n  protected Exception createException(ExceptionGenerator constructorDefinition, Response response) {\n    try {\n      return constructorDefinition.createException(response);\n    } catch (IllegalAccessException e) {\n      throw new IllegalStateException(\"Cannot access constructor\", e);\n    } catch (InstantiationException e) {\n      throw new IllegalStateException(\"Cannot instantiate exception with constructor\", e);\n    } catch (InvocationTargetException e) {\n      throw new IllegalStateException(\"Cannot invoke constructor\", e);\n    } catch (NoSuchMethodException e) {\n      throw new IllegalStateException(\"Constructor does not exist\", e);\n    }\n  }\n}\n","sourceCodeStart":40,"sourceCodeEnd":66,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/annotation-error-decoder/src/main/java/feign/error/MethodErrorHandler.java#L40-L66","documentation":"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.","triggerScenarios":"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.","commonSituations":"Pointing errorHandling at a shared abstract ApiException parent instead of a concrete subclass; refactoring the concrete exception to abstract after introducing subclasses.","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"],"exampleFix":"// before\n@ErrorHandling(codeSpecific = @ErrorCodes(codes = {500}, generate = AbstractApiException.class))\n// after\n@ErrorHandling(codeSpecific = @ErrorCodes(codes = {500}, generate = ConcreteApiException.class))","handlingStrategy":"validation","validationCode":"if (Modifier.isAbstract(ApiError.class.getModifiers()) || ApiError.class.isInterface()) {\n  throw new IllegalStateException(\"generate= must reference a concrete exception class\");\n}","typeGuard":"boolean isConcreteException = !Modifier.isAbstract(cls.getModifiers()) && Throwable.class.isAssignableFrom(cls);","tryCatchPattern":"try { api.call(); } catch (IllegalStateException e) {\n  if (\"Cannot instantiate exception with constructor\".equals(e.getMessage())) { ... }\n}","preventionTips":["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"],"tags":["java","feign","reflection","instantiation"],"backgroundTag":"type-mismatch","analyzedSha":"e2a1e27560a1e68840c34f031afca88b36096e30","analyzedAt":"2026-09-10T12:37:37.238Z","contentChangedAt":"2026-09-10T12:37:37.238Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}