dotnet/wpf · error · FormatException

errMsg (dynamic: message of caught…

Error message

errMsg (dynamic: message of caught FormatException/OverflowException while parsing PrintTicket value)

What it means

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.

Solutions

  1. Inspect the PrintTicket XML for the malformed numeric value and correct it
  2. Regenerate the PrintTicket from the driver defaults instead of reusing the corrupted one
  3. Catch FormatException around PrintTicket loading and rebuild the ticket
  4. Ensure values are written/read with invariant culture and within Int32 range

Example fix

// before
var ticket = new PrintTicket(new MemoryStream(badXmlBytes));
// after
try { var ticket = new PrintTicket(new MemoryStream(badXmlBytes)); }
catch (FormatException) { ticket = printQueue.DefaultPrintTicket; }
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-validate XML numeric attribute before handing to PrintTicket
if (!int.TryParse(attrValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out _)) { /* repair or reject ticket */ }

Type guard

null

Try / catch

try { ticket = new PrintTicket(stream); }
catch (FormatException ex) { log.Warn("Malformed PrintTicket value: " + ex.Message); ticket = printQueue.DefaultPrintTicket; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ef7c2948b8f39b57. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PrintSchema.cs:2445

            try
            {
                intValue = XmlConvert.ToInt32(s);
            }
            catch (FormatException e)
            {
                errMsg = e.Message;
                validValue = false;
            }
            catch (OverflowException e)
            {
                errMsg = e.Message;
                validValue = false;
            }

            if (!validValue)
            {
                throw new FormatException(errMsg);
            }

            return intValue;
        }

        // will generic methods help here?

        /// <summary>
        /// Converts a string to decimal value
        /// </summary>
        /// <exception cref="FormatException">thrown if the string is invalid to be converted</exception>
        public static decimal ConvertStringToDecimal(string s)
        {
            decimal decValue = 0;
            bool validValue = true;
            string errMsg = null;

            try

View on GitHub (pinned to 81131a70a4)