处理 Console.Writeline 内容中的异常(Null?)
Handling Exceptions (Null?) in Console.Writeline content
必须有更好的方法来做到这一点?!我在搞乱?等等,但无法找出合适的上下文。我需要向控制台输出添加更多项,因此添加更多嵌套的 try-catch 是一项艰巨但可行的工作。将不得不处理 4 个可能引发异常的不同项目。
也许,我应该在 writeline 语句之前一段一段地构建字符串?
"for now" 我有这个烂摊子:
foreach (WorkItem workItem in workItems){
// write work item to console
try // ideal both assigned and tagged
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id,
workItem.Fields["System.Title"], workItem.Fields["System.WorkItemType"],
workItem.Fields["System.AssignedTo"], workItem.Fields["System.State"],
workItem.Fields["System.Tags"]);
}
catch (Exception) // at least one not correct, maybe two
{
try // is it the assigned?
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id,
workItem.Fields["System.Title"],
workItem.Fields["System.WorkItemType"], "Unassigned",
workItem.Fields["System.State"], workItem.Fields["System.Tags"]);
}
catch (Exception)
{
try // is it the tags?
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id,
workItem.Fields["System.Title"],
workItem.Fields["System.WorkItemType"],
workItem.Fields["System.AssignedTo"],
workItem.Fields["System.State"], "NoTags");
}
catch (Exception) // its both
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id,
workItem.Fields["System.Title"],
workItem.Fields["System.WorkItemType"], "Unassigned",
workItem.Fields["System.State"], "NoTags");
}
}
}
}
基于评论的讨论。我想总结如下解决方案。
感谢 juharr 和 Selvin 分享想法。
1,因为Fields.Contains("") 抛出错误。您可以使用 workItem.Fields.ContainsKey()
。示例代码如下:
workItem.Fields.ContainsKey("name") ? workItem.Fields["name"] : "default"
2、使用TryGetValue()
workItem.Fields.TryGetValue("System.AssignedTo", out Object identityOjbect)? identityOjbect:"not exist"
必须有更好的方法来做到这一点?!我在搞乱?等等,但无法找出合适的上下文。我需要向控制台输出添加更多项,因此添加更多嵌套的 try-catch 是一项艰巨但可行的工作。将不得不处理 4 个可能引发异常的不同项目。
也许,我应该在 writeline 语句之前一段一段地构建字符串?
"for now" 我有这个烂摊子:
foreach (WorkItem workItem in workItems){
// write work item to console
try // ideal both assigned and tagged
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id,
workItem.Fields["System.Title"], workItem.Fields["System.WorkItemType"],
workItem.Fields["System.AssignedTo"], workItem.Fields["System.State"],
workItem.Fields["System.Tags"]);
}
catch (Exception) // at least one not correct, maybe two
{
try // is it the assigned?
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id,
workItem.Fields["System.Title"],
workItem.Fields["System.WorkItemType"], "Unassigned",
workItem.Fields["System.State"], workItem.Fields["System.Tags"]);
}
catch (Exception)
{
try // is it the tags?
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id,
workItem.Fields["System.Title"],
workItem.Fields["System.WorkItemType"],
workItem.Fields["System.AssignedTo"],
workItem.Fields["System.State"], "NoTags");
}
catch (Exception) // its both
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id,
workItem.Fields["System.Title"],
workItem.Fields["System.WorkItemType"], "Unassigned",
workItem.Fields["System.State"], "NoTags");
}
}
}
}
基于评论的讨论。我想总结如下解决方案。 感谢 juharr 和 Selvin 分享想法。
1,因为Fields.Contains("") 抛出错误。您可以使用 workItem.Fields.ContainsKey()
。示例代码如下:
workItem.Fields.ContainsKey("name") ? workItem.Fields["name"] : "default"
2、使用TryGetValue()
workItem.Fields.TryGetValue("System.AssignedTo", out Object identityOjbect)? identityOjbect:"not exist"