{"record":{"id":"b30b9b8d975a2134","repo":"spring-projects/spring-framework","slug":"constructor-threw-exception","errorCode":null,"errorMessage":"Constructor threw exception","messagePattern":"Constructor threw exception","errorType":"exception","errorClass":"BeanInstantiationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/BeanUtils.java","lineNumber":220,"sourceCode":"\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\targsWithDefaultValues[i] = args[i];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn ctor.newInstance(argsWithDefaultValues);\n\t\t\t}\n\t\t}\n\t\tcatch (InstantiationException ex) {\n\t\t\tthrow new BeanInstantiationException(ctor, \"Is it an abstract class?\", ex);\n\t\t}\n\t\tcatch (IllegalAccessException ex) {\n\t\t\tthrow new BeanInstantiationException(ctor, \"Is the constructor accessible?\", ex);\n\t\t}\n\t\tcatch (IllegalArgumentException ex) {\n\t\t\tthrow new BeanInstantiationException(ctor, \"Illegal arguments for constructor\", ex);\n\t\t}\n\t\tcatch (InvocationTargetException ex) {\n\t\t\tthrow new BeanInstantiationException(ctor, \"Constructor threw exception\", ex.getTargetException());\n\t\t}\n\t}\n\n\t/**\n\t * Return a resolvable constructor for the provided class, either a primary or single\n\t * public constructor with arguments, a single non-public constructor with arguments\n\t * or simply a default constructor.\n\t * <p>Callers have to be prepared to resolve arguments for the returned constructor's\n\t * parameters, if any.\n\t * @param clazz the class to check\n\t * @throws IllegalStateException in case of no unique constructor found at all\n\t * @since 5.3\n\t * @see #findPrimaryConstructor\n\t */\n\t@SuppressWarnings(\"unchecked\")\n\tpublic static <T> Constructor<T> getResolvableConstructor(Class<T> clazz) {\n\t\tConstructor<T> ctor = findPrimaryConstructor(clazz);\n\t\tif (ctor != null) {","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L202-L238","documentation":"Thrown as BeanInstantiationException by instantiateClass(Constructor, args) at BeanUtils.java:220 when Constructor.newInstance raises InvocationTargetException — i.e. the constructor ran and the constructed object's constructor body threw. The framework unwraps it via ex.getTargetException() and stores that as the cause, so the real application exception is preserved.","triggerScenarios":"The bean's constructor itself throws any Throwable (NullPointerException, IllegalStateException, IllegalArgumentException, an application domain exception, a failed assertion, an assertion in a required field check). The constructor was found and invoked but the object refused to initialize.","commonSituations":"Constructor that validates arguments and throws on bad input; constructor performing I/O/DB calls that fail; @PostConstruct-style logic accidentally placed in the constructor; null-checks with Objects.requireNonNull on missing dependencies; failing static initialization triggered during construction.","solutions":["Inspect getCause()/getRootCause() of the BeanInstantiationException — it is the original exception from the constructor.","Reproduce by calling 'new Bean(args...)' directly to confirm the constructor throws.","Fix the invariants/inputs so the constructor succeeds, or move side-effecting logic out of the constructor.","Ensure required dependencies/fields are supplied (non-null) to the constructor."],"exampleFix":"// before\npublic UserService(Repo repo) {\n    this.repo = Objects.requireNonNull(repo);\n    this.cache = repo.loadAll(); // throws if DB down\n}\n\n// after\npublic UserService(Repo repo) {\n    this.repo = Objects.requireNonNull(repo);\n    this.cache = new HashMap<>(); // lazy-load later\n}","handlingStrategy":"try-catch","validationCode":"// Smoke-test the constructor with sample/validated args\ntry {\n    Constructor<T> c = clazz.getDeclaredConstructor(...);\n    // validate inputs against @NotNull/Bean Validation before invoking\n} catch (Exception ignore) {}","typeGuard":"// No static guard possible for arbitrary constructor side effects;\n// wrap calls and inspect the cause.\nstatic <T> Optional<T> safeNew(Constructor<T> c, Object[] args) {\n    try { return Optional.of(BeanUtils.instantiateClass(c, args)); }\n    catch (BeanInstantiationException e) { return Optional.empty(); }\n}","tryCatchPattern":"try {\n    BeanUtils.instantiateClass(ctor, args);\n} catch (BeanInstantiationException ex) {\n    Throwable real = ex.getCause(); // original exception from ctor body\n    // classify: missing dependency, bad input, or environment failure\n    throw new IllegalStateException(\"constructor failed\", real);\n}","preventionTips":["Keep constructors side-effect free; move I/O/DB work to @PostConstruct or init methods.","Validate required arguments with Objects.requireNonNull and clear messages.","Reproduce failures by calling 'new Bean(...)' directly in a test.","Always unwrap getCause() of BeanInstantiationException to see the real error."],"tags":["spring-beans","beanutils","instantiation","constructor","runtime-exception","reflection"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}