在c#中,我可以写这样的东西:
if (
(
try {
...
return true;
}
catch (Exception ex)
{
return false;
}
) == true
)
{
...
}
无需将我所有的 try/catch 块移动到新函数中
- 编辑 -
好的。我完成了我的问题。(也许回答一下)。try/catch 中应该包含的是一种 XmlDocument.TryLoad(stream)(就像有一个 int.tryParse(string))。我只需要一次,所以我想避免做一个额外的功能。所以我的代码会是这样的
try {
new XmlDocument().Load(foo);
return true;
}
catch (Exception ex)
{
return false;
}
我只想知道会不会出错。我不在乎原因(流空,编码错误)。
有很多有趣的答案,但我认为更适合我的是为 xmlDocument 创建扩展方法。这将比在我的声明中强制使用匿名方法更干净(并且可重用且更易于阅读)