我使用属性设置器来验证 C# 类中的输入并在无效输入上抛出异常。我还使用 Json.NET 将 json 反序列化为对象。问题是我不知道在哪里捕获 setter 抛出的无效 json 值的异常。异常不会从JsonConvert.DeserializeObject方法中抛出。
public class A{
    private string a;
    public string number{
        get {return a;}
        set {
            if (!Regex.IsMatch(value, "^\\d+$"))
                throw new Exception();
            a = value;
        }
    }
}
public class Main
{
    public static void main()
    {
         // The Exception cannot be caught here.
         A a = JsonConvert.DeserializeObject<A>("{number:'some thing'}");
    }    
}