studyzy/imewlconverter · error · Exception

找不到拼音

Error message

找不到拼音

What it means

ChaoyinHelper.GetChaoyin(string) converts one pinyin syllable into a Chaoyin (超音速写) code. It throws this bare Exception ONLY as a null/empty precondition guard on the input pinyin. Note the method returns null (does not throw) when the pinyin is well-formed but simply absent from the mapping, so this specific throw means the caller passed NO pinyin at all, not an unknown one.

Source

Thrown at src/ImeWlConverter.Core/Helpers/ChaoyinHelper.cs:45

                    .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var line in lines)
                {
                    var array = line.Split('\t');
                    pinyinCodeMapping.Add(array[0], array[1]);
                    if (array[2] == "Y") ShenmuY.Add(array[0]);
                }
            }

            return pinyinCodeMapping;
        }
    }

    /// <summary>
    /// 获得一个拼音对应的超音编码
    /// </summary>
    public static string? GetChaoyin(string pinyin)
    {
        if (string.IsNullOrEmpty(pinyin)) throw new Exception("找不到拼音");
        var yindiao = 10;
        if (regex.IsMatch(pinyin))
        {
            yindiao = Convert.ToInt32(pinyin[pinyin.Length - 1].ToString());
            pinyin = pinyin.Substring(0, pinyin.Length - 1);
        }

        if (!PinyinCodeMapping.ContainsKey(pinyin))
        {
            Debug.WriteLine("Can not find Chaoyin code by pinyin=" + pinyin);
            return null;
        }

        var zy = PinyinCodeMapping[pinyin];
        Debug.WriteLine("Pinyin:" + pinyin + ",Chaoyin:" + zy);
        return zy;
    }

View on GitHub (pinned to 16744a12ed)

Solutions

  1. Filter empty/whitespace strings out of the pinyin list before calling Chaoyin generation.
  2. Guard each pinyin with string.IsNullOrWhiteSpace(p) and skip the word/code when blank.
  3. Fix upstream pinyin resolution (PinyinHelper) so empty pinyins never reach the generator.

Example fix

// before
var code = ChaoyinHelper.GetChaoyin(p);
// after
if (string.IsNullOrWhiteSpace(p)) continue;
var code = ChaoyinHelper.GetChaoyin(p);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(pinyin)) continue; // or return null
var code = ChaoyinHelper.GetChaoyin(pinyin);

Try / catch

try { var code = ChaoyinHelper.GetChaoyin(p); }
catch (Exception ex) when (ex.Message == "找不到拼音") { /* skip empty pinyin */ }

Prevention

When it happens

Trigger: Calling ChaoyinHelper.GetChaoyin(null) or GetChaoyin("") directly; or indirectly via the overload GetChaoyin(IList<string>) which at line 70 forwards pinyins[0] when the list has one element — if that element is an empty string, the throw fires.

Common situations: Running Chaoyin code generation on a word whose pinyin could not be resolved upstream, so an empty pinyin was propagated into the code list. Also when an importer emits a blank pinyin code that is later fed into the Chaoyin generator.

Related errors


AI-assisted analysis of studyzy/imewlconverter@16744a12ed (2026-08-13). Data as JSON: /api/errors/62df624992fe5a84. Report an issue: GitHub.