我有一些代码可以将JSON读取到动态对象中,如下所示:
dynamic listOfThings = JsonConvert.DeserializeObject(listOfThingsJson);
我正在像这样遍历它们:
foreach(dynamic thing in listOfThings) {
string propertyOne = thing.PropertyOne;
string propertyTwo = thing.PropertyTwo;
doWork(propertyOne, propertyTwo);
}
我如何:如果在foreach语句本身或任何一个属性访问语句中遇到RuntimeBinderException,但是只是忽略该迭代的执行并继续循环,如何捕获?
类似的东西:
foreach(dynamic thing in listOfThings) { \\if a RuntimeBinderException is thrown on this line
string propertyOne = thing.PropertyOne \\or on this line
string propertyTwo = thing.PropertyTwo \\or on this line, catch the exception
doWork(propertyOne, propertyTwo) \\and move to the next iteration
答案 0 :(得分:1)
不确定您为什么不能使用try .. catch
块
foreach(dynamic thing in listOfThings) {
try {
string propertyOne = thing.PropertyOne;
string propertyTwo = thing.PropertyTwo;
doWork(propertyOne, propertyTwo);
}
catch(RuntimeBinderException ex)
{
//log the exception
continue;
}
}