{"record":{"id":"7f9491bb177beda2","repo":"JamesNK/Newtonsoft.Json","slug":"error-creating-0","errorCode":null,"errorMessage":"Error creating '{0}'.","messagePattern":"Error creating '(.+?)'\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs","lineNumber":307,"sourceCode":"                            ObjectConstructor<object> parameterizedConstructor = ReflectionDelegateFactory.CreateParameterizedConstructor(parameterizedConstructorInfo);\n                            return parameterizedConstructor(parameters);\n                        }\n                        else\n                        {\n                            throw new JsonException(\"No matching parameterized constructor found for '{0}'.\".FormatWith(CultureInfo.InvariantCulture, type));\n                        }\n                    }\n\n                    if (defaultConstructor == null)\n                    {\n                        throw new JsonException(\"No parameterless constructor defined for '{0}'.\".FormatWith(CultureInfo.InvariantCulture, type));\n                    }\n\n                    return defaultConstructor();\n                }\n                catch (Exception ex)\n                {\n                    throw new JsonException(\"Error creating '{0}'.\".FormatWith(CultureInfo.InvariantCulture, type), ex);\n                }\n            };\n        }\n\n#if !(NET20 || DOTNET)\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        private static Type? GetAssociatedMetadataType(Type type)\n        {\n            return AssociatedMetadataTypesCache.Instance.Get(type);\n        }\n\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        private static Type? GetAssociateMetadataTypeFromAttribute(Type type)\n        {\n            Attribute[] customAttributes = ReflectionUtils.GetAttributes(type, null, true);\n","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs#L289-L325","documentation":"Catch-all thrown by GetCreator's lambda wrapping any exception raised while invoking the chosen constructor (default or parameterized). '{0}' is the type being created; the real cause is attached as InnerException. This is the outer envelope whenever converter/naming-strategy instantiation fails for any reason other than the no-match/no-default cases (230, 231).","triggerScenarios":"The constructor of a JsonConverter or NamingStrategy throws (e.g. ArgumentException for an invalid format string, ArgumentNullException, custom validation throwing), or reflection invocation throws TargetInvocationException wrapping a constructor failure.","commonSituations":"A converter constructor validates its arguments and throws on bad input (e.g. invalid date format string), a naming-strategy that throws on null arguments, or a converter whose constructor depends on a service that isn't available. Distinct from 230/231 in that a matching constructor exists but threw.","solutions":["Inspect InnerException to find the constructor's real exception and message.","Correct the arguments passed via [JsonConverter(typeof(T), ...)] so the constructor does not throw.","Make the converter constructor tolerate/validate its inputs gracefully, or move validation out of the constructor.","If the constructor needs runtime services, register a pre-built converter instance via settings.Converters instead of the attribute."],"exampleFix":"// before: ctor throws on bad format\n[JsonConverter(typeof(DateFmt), \"not-a-format\")]\npublic DateTime When { get; set; }\npublic DateFmt(string f) { _fmt = DateTime.ParseExact(DateTime.Now.ToString(), f, null); }\n// after: ctor stores format, parsing deferred\npublic DateFmt(string f) { _fmt = f; }","handlingStrategy":"try-catch","validationCode":"if (type.GetConstructor(args?.Select(a=>a.GetType()).ToArray() ?? Type.EmptyTypes == null ? Type.EmptyTypes : args.Select(a=>a.GetType()).ToArray()) == null) throw new ArgumentException(\"no matching ctor\");","typeGuard":null,"tryCatchPattern":"try { var conv = (JsonConverter)Activator.CreateInstance(type, args); }\ncatch (TargetInvocationException ex) { logger.Error(ex.InnerException, \"converter ctor threw\"); throw; }\n// then for the Json.NET path:\ntry { JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings{ Converters = { ... } }); }\ncatch (JsonException ex) when (ex.Message.Contains(\"Error creating\")) { logger.Error(ex.InnerException, \"converter instantiation failed\"); throw; }","preventionTips":["Make converter constructors validate arguments defensively without throwing.","Keep converter configuration outside the constructor where possible (init methods).","Register fully-configured converter instances via settings.Converters when constructor needs runtime services.","Unit-test converter instantiation with realistic argument values."],"tags":["converter","naming-strategy","reflection","constructor","runtime-exception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}