参数异常无效
本文关键字:无效 异常 参数 | 更新日期: 2023-09-27 18:30:51
我正在开发导航Windows Phone 8.1应用程序。我使用尝试捕获块进行错误处理。一旦我向服务器发送无效输入,例如"dfkdsf",它就会向我抛出一个运行时异常错误,指出参数无效,但 catch 块不处理它。我哪里出错了?
public async void GetSearchAPIData()
{
try
{
JArray arr = JArray.Parse(await responce.Content.ReadAsStringAsync());
foreach (JObject obj in arr.Children<JObject>()) //exception thrown here!
{
noOfResult++;
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
settings.MissingMemberHandling = MissingMemberHandling.Ignore;
var rcvdData = JsonConvert.DeserializeObject<LocationData>(obj.ToString(), settings);
//adding recieved data into a list..
RcvdSearchDataList.Add(rcvdData);
}
//changing current status on status bar..
statusBar.ProgressIndicator.Text = "Found " + noOfResult + " results for " + "'"" + UserRequestedLocation + "'"";
statusBar.ProgressIndicator.ProgressValue = 0;
if (RcvdSearchDataList.Count == 0)
{
statusBar.ProgressIndicator.Text = "We're Sorry! No matches found...";
statusBar.ProgressIndicator.ProgressValue = 0;
}
else
{
ResultListView = new ListView() { Margin = new Thickness(20), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch };
ResultDataGrid.Children.Add(ResultListView);
for (int i = 0; i < RcvdSearchDataList.Count; i++)
{
UpdateSearchTextData(i);
}
}
UpdateSearchMapData(RcvdSearchDataList);
}
catch(Exception e)
{
string errorMessage = string.Format(
"An error occurred while looking for the requested location.'r'n'r'n{0:x}'r'n'r'n{1}", "HRESULT: " + e.HResult, "Message: " + e.Message);
ErrorDialog.Content = errorMessage;
await ErrorDialog.ShowAsync();
}
}
JArray arr 从服务器返回的值为 {[]},我无法处理
注释该行settings.NullValueHandling = NullValueHandling.Ignore
. 您告诉 Json 序列化程序在去浆化期间忽略空值。因此,当您获得空的 Json repsonse 时,它没有反序列化它,并以某种方式导致了无效的参数异常。