studyzy/imewlconverter · error · Exception

找不到字:'{c}'的拼音

Error message

找不到字:'{c}'的拼音

What it means

PinyinHelper.GetDefaultPinyin(char) returns the first pinyin of a character. THIS throw (line 27, inside the try) fires only when the character IS present in PinYinDict but its pinyin list is null or empty — i.e. the dictionary entry exists yet carries no usable pinyin. This is a data-integrity edge, not a normal input case (contrast with error [5]).

Source

Thrown at src/ImeWlConverter.Core/Helpers/PinyinHelper.cs:27

    /// 获得一个字的默认拼音(不包含音调)
    /// </summary>
    public static string GetDefaultPinyin(char c)
    {
        try
        {
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
            {
                return c.ToString().ToLower();
            }

            if (c >= '0' && c <= '9')
            {
                return c.ToString();
            }

            var pys = PinYinDict[c];
            if (pys != null && pys.Count > 0) return pys[0];
            throw new Exception($"找不到字:'{c}'的拼音");
        }
        catch
        {
            throw new Exception($"找不到字:'{c}'的拼音");
        }
    }

    public static IList<string> GetDefaultPinyin(string word)
    {
        var result = new List<string>();
        var si = new StringInfo(word);
        for (int i = 0; i < si.LengthInTextElements; i++)
        {
            var textElement = si.SubstringByTextElements(i, 1);
            if (textElement.Length == 1)
            {
                result.Add(GetDefaultPinyin(textElement[0]));
            }

View on GitHub (pinned to 16744a12ed)

Solutions

  1. Rebuild/restore the project so the embedded ChineseCode.txt resource is intact.
  2. Pre-check: use PinYinDict.TryGetValue(c, out var pys) && pys is { Count: > 0 } before calling.
  3. Wrap the call in try/catch and skip the character.

Example fix

// before
var py = PinyinHelper.GetDefaultPinyin(c);
// after
if (PinyinHelper.PinYinDict.TryGetValue(c, out var pys) && pys is { Count: > 0 })
    var py = pys[0];
else
    continue;
Defensive patterns

Strategy: validation

Validate before calling

if (PinyinHelper.PinYinDict.TryGetValue(c, out var pys) && pys is { Count: > 0 })
    var py = pys[0];
else continue;

Try / catch

try { var py = PinyinHelper.GetDefaultPinyin(c); }
catch (Exception ex) when (ex.Message.StartsWith("找不到字")) { /* skip */ }

Prevention

When it happens

Trigger: Calling GetDefaultPinyin for a character that exists in ChineseCode.txt with an empty pinyin column; a corrupted/truncated embedded ChineseCode.txt resource where a row has a blank pinyin field.

Common situations: A modified or corrupted embedded ChineseCode.txt resource; extremely unusual with the shipped data since all rows normally carry pinyin.

Related errors


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