SubtitleEdit/subtitleedit · error · InvalidOperationException

Parse error in event #{i + 1} style: {ex.Message}

Error message

Parse error in event #{i + 1} style: {ex.Message}

What it means

Wraps an exception raised while parsing the inline 'styles' array of a CLQTT event — specifically when converting the 'from'/'to' character offsets to int (int.Parse) fails, or the substring slicing based on those offsets is out of range. The inner exception message is embedded in the thrown InvalidOperationException.

Source

Thrown at src/libse/SubtitleFormats/ClqttJson.cs:145

                            foreach (var styleJson in styles)
                            {
                                var styleType = parser.GetFirstObject(styleJson, "type");
                                if (styleType.Equals("italic", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    try
                                    {
                                        var from = int.Parse(parser.GetFirstObject(styleJson, "from"));
                                        var to = int.Parse(parser.GetFirstObject(styleJson, "to"));

                                        subText = subText.Substring(0, from)
                                                    + "<i>"
                                                    + subText.Substring(from, to - from)
                                                    + "</i>"
                                                    + subText.Substring(to);
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new InvalidOperationException($"Parse error in event #{i + 1} style: {ex.Message}", ex);
                                    }
                                }
                            }

                            var subTextPrefix = string.Empty;
                            if (region.Equals("top", StringComparison.InvariantCultureIgnoreCase))
                            {
                                subTextPrefix = "{\\an8}";
                            }

                            subText = subText.Replace("\r\n", Environment.NewLine).Replace("\n", Environment.NewLine);
                            subText = subTextPrefix + Json.DecodeJsonText(subText);

                            var p = new Paragraph(subText, FramesToMilliseconds(start, frameRate), FramesToMilliseconds(end, frameRate));
                            p.Region = region;

                            if (annotations.Count > 0)
                            {

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Inspect event #{i+1} in the .clqtt file and verify each style's 'from' and 'to' are valid integers within the text length and from <= to.
  2. Remove or regenerate the style entries for the offending event.
  3. If producing CLQTT, recompute style offsets after any text edit.

Example fix

// before
"styles": [{ "from": "abc", "to": "5" }]
// after
"styles": [{ "from": 0, "to": 5 }]
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate style offsets against the text length before parsing
var txt = parser.GetFirstObject(eventJson, "txt");
foreach (var styleJson in styles)
{
    if (!int.TryParse(parser.GetFirstObject(styleJson, "from"), out var from)
        || !int.TryParse(parser.GetFirstObject(styleJson, "to"), out var to)
        || from < 0 || to > txt.Length || from > to)
    {
        // skip or fix the malformed style
        continue;
    }
}

Try / catch

try
{
    new ClqttJson().LoadSubtitle(sub, lines, fileName);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Parse error in event") && ex.Message.Contains("style"))
{
    // ex.Message includes the event number and inner cause
    logger.Error(ex, "CLQTT style parse failed");
}

Prevention

When it happens

Trigger: A CLQTT event whose styles[].from or styles[].to fields are non-numeric, empty, or where 'to' < 'from', causing int.Parse or the subsequent Substring calls to throw. The 1-based event index is included to locate the offending entry.

Common situations: Malformed style offsets from a buggy exporter; CLQTT files edited by hand with wrong index values; text that was shortened after style offsets were computed (stale offsets).

Understand the failure class

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/06773f79d218135a. Report an issue: GitHub.