{"record":{"id":"ef7c2948b8f39b57","repo":"dotnet/wpf","slug":"errmsg-dynamic-message-of-caught-formatexception","errorCode":null,"errorMessage":"errMsg (dynamic: message of caught FormatException/OverflowException while parsing PrintTicket value)","messagePattern":"errMsg \\(dynamic: message of caught FormatException/OverflowException while parsing PrintTicket value\\)","errorType":"validation","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PrintSchema.cs","lineNumber":2445,"sourceCode":"\n            try\n            {\n                intValue = XmlConvert.ToInt32(s);\n            }\n            catch (FormatException e)\n            {\n                errMsg = e.Message;\n                validValue = false;\n            }\n            catch (OverflowException e)\n            {\n                errMsg = e.Message;\n                validValue = false;\n            }\n\n            if (!validValue)\n            {\n                throw new FormatException(errMsg);\n            }\n\n            return intValue;\n        }\n\n        // will generic methods help here?\n\n        /// <summary>\n        /// Converts a string to decimal value\n        /// </summary>\n        /// <exception cref=\"FormatException\">thrown if the string is invalid to be converted</exception>\n        public static decimal ConvertStringToDecimal(string s)\n        {\n            decimal decValue = 0;\n            bool validValue = true;\n            string errMsg = null;\n\n            try","sourceCodeStart":2427,"sourceCodeEnd":2463,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PrintSchema.cs#L2427-L2463","documentation":"PrintSchema's internal integer parser converts a PrintTicket XML attribute value to an int. If int.Parse throws FormatException or OverflowException, the original exception message is rethrown as a FormatException (errMsg). This indicates malformed or out-of-range numeric data inside a PrintTicket document.","triggerScenarios":"Parsing a PrintTicket XML file whose numeric property (e.g. a resolution or count value) is non-numeric or exceeds Int32 range; hand-edited or vendor-generated malformed PrintTicket XML.","commonSituations":"Opening PrintTicket files produced by third-party drivers; corrupted saved print settings; XML where a value was localized (e.g. '1,000' or '1.000') and cannot be parsed with invariant culture.","solutions":["Inspect the PrintTicket XML for the malformed numeric value and correct it","Regenerate the PrintTicket from the driver defaults instead of reusing the corrupted one","Catch FormatException around PrintTicket loading and rebuild the ticket","Ensure values are written/read with invariant culture and within Int32 range"],"exampleFix":"// before\nvar ticket = new PrintTicket(new MemoryStream(badXmlBytes));\n// after\ntry { var ticket = new PrintTicket(new MemoryStream(badXmlBytes)); }\ncatch (FormatException) { ticket = printQueue.DefaultPrintTicket; }","handlingStrategy":"try-catch","validationCode":"// Pre-validate XML numeric attribute before handing to PrintTicket\nif (!int.TryParse(attrValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out _)) { /* repair or reject ticket */ }","typeGuard":"null","tryCatchPattern":"try { ticket = new PrintTicket(stream); }\ncatch (FormatException ex) { log.Warn(\"Malformed PrintTicket value: \" + ex.Message); ticket = printQueue.DefaultPrintTicket; }","preventionTips":["Write PrintTicket numeric values using invariant culture","Do not hand-edit PrintTicket XML","Regenerate tickets from driver defaults after corruption"],"tags":["wpf","printing","formatexception","xml","parsing"],"backgroundTag":"invalid-argument-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}