gpt4 book ai didi

c# - 无法捕获 JsonReaderException

转载 作者:太空宇宙 更新时间:2023-11-03 15:29:24 25 4
gpt4 key购买 nike

我在部署的系统上收到以下错误:

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: S. Path '', line 0, position 0.
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonTextReader.ReadInternal()
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at JsonWrapper.DeserializeJson[T](String data, ITraceWriter tracer)

这是来自 DLL 的简化 JsonWrapper.DerserializeJson 代码:

public static T DeserializeJson<T>(string data, ITraceWriter tracer) {
try {
return JsonConvert.DeserializeObject<T>(data, new JsonSerializerSettings { TraceWriter = tracer });
} catch (JsonReaderException jrex) {
throw new CustomException("Unable to deserialize Json message.", jrex);
}
}

我知道为什么抛出异常,但是我无法弄清楚为什么它没有被 try-catch block 捕获。

此代码是我们的一位客户正在使用的 DLL。我希望这是一个可以单独在 DLL 中完成的修复。

任何对此的见解都会很棒!

最佳答案

您的 JsonException 可能来源于另一个程序集。

如果您的 Newtonsoft 版本与其他程序集中的版本不同,特定的 catch block 将不会捕获它(通用异常捕获)。

解决方案是使用相同版本的 Newtonsoft。

dbc 在评论中提到了这一点。

关于c# - 无法捕获 JsonReaderException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34234433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com